video_reader.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "./ivfdec.h"
  13. #include "./video_reader.h"
  14. #include "vpx_ports/mem_ops.h"
  15. static const char *const kIVFSignature = "DKIF";
  16. struct VpxVideoReaderStruct {
  17. VpxVideoInfo info;
  18. FILE *file;
  19. uint8_t *buffer;
  20. size_t buffer_size;
  21. size_t frame_size;
  22. };
  23. VpxVideoReader *vpx_video_reader_open(const char *filename) {
  24. char header[32];
  25. VpxVideoReader *reader = NULL;
  26. FILE *const file = fopen(filename, "rb");
  27. if (!file) {
  28. fprintf(stderr, "%s can't be opened.\n", filename); // Can't open file
  29. return NULL;
  30. }
  31. if (fread(header, 1, 32, file) != 32) {
  32. fprintf(stderr, "File header on %s can't be read.\n",
  33. filename); // Can't read file header
  34. return NULL;
  35. }
  36. if (memcmp(kIVFSignature, header, 4) != 0) {
  37. fprintf(stderr, "The IVF signature on %s is wrong.\n",
  38. filename); // Wrong IVF signature
  39. return NULL;
  40. }
  41. if (mem_get_le16(header + 4) != 0) {
  42. fprintf(stderr, "%s uses the wrong IVF version.\n",
  43. filename); // Wrong IVF version
  44. return NULL;
  45. }
  46. reader = calloc(1, sizeof(*reader));
  47. if (!reader) {
  48. fprintf(
  49. stderr,
  50. "Can't allocate VpxVideoReader\n"); // Can't allocate VpxVideoReader
  51. return NULL;
  52. }
  53. reader->file = file;
  54. reader->info.codec_fourcc = mem_get_le32(header + 8);
  55. reader->info.frame_width = mem_get_le16(header + 12);
  56. reader->info.frame_height = mem_get_le16(header + 14);
  57. reader->info.time_base.numerator = mem_get_le32(header + 16);
  58. reader->info.time_base.denominator = mem_get_le32(header + 20);
  59. return reader;
  60. }
  61. void vpx_video_reader_close(VpxVideoReader *reader) {
  62. if (reader) {
  63. fclose(reader->file);
  64. free(reader->buffer);
  65. free(reader);
  66. }
  67. }
  68. int vpx_video_reader_read_frame(VpxVideoReader *reader) {
  69. return !ivf_read_frame(reader->file, &reader->buffer, &reader->frame_size,
  70. &reader->buffer_size);
  71. }
  72. const uint8_t *vpx_video_reader_get_frame(VpxVideoReader *reader,
  73. size_t *size) {
  74. if (size) *size = reader->frame_size;
  75. return reader->buffer;
  76. }
  77. const VpxVideoInfo *vpx_video_reader_get_info(VpxVideoReader *reader) {
  78. return &reader->info;
  79. }