video_reader.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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) return NULL; // Can't open file
  28. if (fread(header, 1, 32, file) != 32) return NULL; // Can't read file header
  29. if (memcmp(kIVFSignature, header, 4) != 0)
  30. return NULL; // Wrong IVF signature
  31. if (mem_get_le16(header + 4) != 0) return NULL; // Wrong IVF version
  32. reader = calloc(1, sizeof(*reader));
  33. if (!reader) return NULL; // Can't allocate VpxVideoReader
  34. reader->file = file;
  35. reader->info.codec_fourcc = mem_get_le32(header + 8);
  36. reader->info.frame_width = mem_get_le16(header + 12);
  37. reader->info.frame_height = mem_get_le16(header + 14);
  38. reader->info.time_base.numerator = mem_get_le32(header + 16);
  39. reader->info.time_base.denominator = mem_get_le32(header + 20);
  40. return reader;
  41. }
  42. void vpx_video_reader_close(VpxVideoReader *reader) {
  43. if (reader) {
  44. fclose(reader->file);
  45. free(reader->buffer);
  46. free(reader);
  47. }
  48. }
  49. int vpx_video_reader_read_frame(VpxVideoReader *reader) {
  50. return !ivf_read_frame(reader->file, &reader->buffer, &reader->frame_size,
  51. &reader->buffer_size);
  52. }
  53. const uint8_t *vpx_video_reader_get_frame(VpxVideoReader *reader,
  54. size_t *size) {
  55. if (size) *size = reader->frame_size;
  56. return reader->buffer;
  57. }
  58. const VpxVideoInfo *vpx_video_reader_get_info(VpxVideoReader *reader) {
  59. return &reader->info;
  60. }