ivfdec.c 3.24 KB
Newer Older
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 11 12 13
 */

#include <stdio.h>
#include <stdlib.h>
14 15
#include <string.h>

16
#include "aom_ports/mem_ops.h"
17

18 19 20
#include "./ivfdec.h"

static const char *IVF_SIGNATURE = "DKIF";
21

Dmitry Kovalev's avatar
Dmitry Kovalev committed
22
static void fix_framerate(int *num, int *den) {
Yaowu Xu's avatar
Yaowu Xu committed
23
  // Some versions of aomenc used 1/(2*fps) for the timebase, so
Dmitry Kovalev's avatar
Dmitry Kovalev committed
24 25 26
  // we can guess the framerate using only the timebase in this
  // case. Other files would require reading ahead to guess the
  // timebase, like we do for webm.
27
  if (*den > 0 && *den < 1000000000 && *num > 0 && *num < 1000) {
Dmitry Kovalev's avatar
Dmitry Kovalev committed
28 29 30 31 32 33 34 35 36 37 38 39 40
    // Correct for the factor of 2 applied to the timebase in the encoder.
    if (*num & 1)
      *den *= 2;
    else
      *num /= 2;
  } else {
    // Don't know FPS for sure, and don't have readahead code
    // (yet?), so just default to 30fps.
    *num = 30;
    *den = 1;
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
41
int file_is_ivf(struct AvxInputContext *input_ctx) {
42 43 44 45
  char raw_hdr[32];
  int is_ivf = 0;

  if (fread(raw_hdr, 1, 32, input_ctx->file) == 32) {
46
    if (memcmp(IVF_SIGNATURE, raw_hdr, 4) == 0) {
47 48 49
      is_ivf = 1;

      if (mem_get_le16(raw_hdr + 4) != 0) {
clang-format's avatar
clang-format committed
50 51
        fprintf(stderr,
                "Error: Unrecognized IVF version! This file may not"
52 53 54 55 56 57 58 59
                " decode properly.");
      }

      input_ctx->fourcc = mem_get_le32(raw_hdr + 8);
      input_ctx->width = mem_get_le16(raw_hdr + 12);
      input_ctx->height = mem_get_le16(raw_hdr + 14);
      input_ctx->framerate.numerator = mem_get_le32(raw_hdr + 16);
      input_ctx->framerate.denominator = mem_get_le32(raw_hdr + 20);
Dmitry Kovalev's avatar
Dmitry Kovalev committed
60 61
      fix_framerate(&input_ctx->framerate.numerator,
                    &input_ctx->framerate.denominator);
62 63 64
    }
  }

65
  if (!is_ivf) {
66
    rewind(input_ctx->file);
67 68
    input_ctx->detect.buf_read = 0;
  } else {
69
    input_ctx->detect.position = 4;
70
  }
71 72 73
  return is_ivf;
}

clang-format's avatar
clang-format committed
74 75 76
int ivf_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
                   size_t *buffer_size) {
  char raw_header[IVF_FRAME_HDR_SZ] = { 0 };
77 78 79
  size_t frame_size = 0;

  if (fread(raw_header, IVF_FRAME_HDR_SZ, 1, infile) != 1) {
clang-format's avatar
clang-format committed
80
    if (!feof(infile)) warn("Failed to read frame size\n");
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  } else {
    frame_size = mem_get_le32(raw_header);

    if (frame_size > 256 * 1024 * 1024) {
      warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
      frame_size = 0;
    }

    if (frame_size > *buffer_size) {
      uint8_t *new_buffer = realloc(*buffer, 2 * frame_size);

      if (new_buffer) {
        *buffer = new_buffer;
        *buffer_size = 2 * frame_size;
      } else {
        warn("Failed to allocate compressed data buffer\n");
        frame_size = 0;
      }
    }
  }

  if (!feof(infile)) {
    if (fread(*buffer, 1, frame_size, infile) != frame_size) {
      warn("Failed to read full frame\n");
      return 1;
    }

    *bytes_read = frame_size;
    return 0;
  }

  return 1;
}