2
0

ivfdec.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2013 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 <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "vpx_ports/mem_ops.h"
  14. #include "./ivfdec.h"
  15. static const char *IVF_SIGNATURE = "DKIF";
  16. static void fix_framerate(int *num, int *den) {
  17. // Some versions of vpxenc used 1/(2*fps) for the timebase, so
  18. // we can guess the framerate using only the timebase in this
  19. // case. Other files would require reading ahead to guess the
  20. // timebase, like we do for webm.
  21. if (*den > 0 && *den < 1000000000 && *num > 0 && *num < 1000) {
  22. // Correct for the factor of 2 applied to the timebase in the encoder.
  23. if (*num & 1)
  24. *den *= 2;
  25. else
  26. *num /= 2;
  27. } else {
  28. // Don't know FPS for sure, and don't have readahead code
  29. // (yet?), so just default to 30fps.
  30. *num = 30;
  31. *den = 1;
  32. }
  33. }
  34. int file_is_ivf(struct VpxInputContext *input_ctx) {
  35. char raw_hdr[32];
  36. int is_ivf = 0;
  37. if (fread(raw_hdr, 1, 32, input_ctx->file) == 32) {
  38. if (memcmp(IVF_SIGNATURE, raw_hdr, 4) == 0) {
  39. is_ivf = 1;
  40. if (mem_get_le16(raw_hdr + 4) != 0) {
  41. fprintf(stderr,
  42. "Error: Unrecognized IVF version! This file may not"
  43. " decode properly.");
  44. }
  45. input_ctx->fourcc = mem_get_le32(raw_hdr + 8);
  46. input_ctx->width = mem_get_le16(raw_hdr + 12);
  47. input_ctx->height = mem_get_le16(raw_hdr + 14);
  48. input_ctx->framerate.numerator = mem_get_le32(raw_hdr + 16);
  49. input_ctx->framerate.denominator = mem_get_le32(raw_hdr + 20);
  50. fix_framerate(&input_ctx->framerate.numerator,
  51. &input_ctx->framerate.denominator);
  52. }
  53. }
  54. if (!is_ivf) {
  55. rewind(input_ctx->file);
  56. input_ctx->detect.buf_read = 0;
  57. } else {
  58. input_ctx->detect.position = 4;
  59. }
  60. return is_ivf;
  61. }
  62. int ivf_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
  63. size_t *buffer_size) {
  64. char raw_header[IVF_FRAME_HDR_SZ] = { 0 };
  65. size_t frame_size = 0;
  66. if (fread(raw_header, IVF_FRAME_HDR_SZ, 1, infile) != 1) {
  67. if (!feof(infile)) warn("Failed to read frame size");
  68. } else {
  69. frame_size = mem_get_le32(raw_header);
  70. if (frame_size > 256 * 1024 * 1024) {
  71. warn("Read invalid frame size (%u)", (unsigned int)frame_size);
  72. frame_size = 0;
  73. }
  74. if (frame_size > *buffer_size) {
  75. uint8_t *new_buffer = realloc(*buffer, 2 * frame_size);
  76. if (new_buffer) {
  77. *buffer = new_buffer;
  78. *buffer_size = 2 * frame_size;
  79. } else {
  80. warn("Failed to allocate compressed data buffer");
  81. frame_size = 0;
  82. }
  83. }
  84. }
  85. if (!feof(infile)) {
  86. if (fread(*buffer, 1, frame_size, infile) != frame_size) {
  87. warn("Failed to read full frame");
  88. return 1;
  89. }
  90. *bytes_read = frame_size;
  91. return 0;
  92. }
  93. return 1;
  94. }