ivfdec.c 4.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 *  Copyright (c) 2013 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <stdio.h>
#include <stdlib.h>
13 14 15 16 17
#include <string.h>

#include "./ivfdec.h"

static const char *IVF_SIGNATURE = "DKIF";
18

Dmitry Kovalev's avatar
Dmitry Kovalev committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
static void fix_framerate(int *num, int *den) {
  // Some versions of vpxenc used 1/(2*fps) for the timebase, so
  // 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.
  if (*num < 1000) {
    // 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;
  }
}

38 39 40 41 42
int file_is_ivf(struct VpxInputContext *input_ctx) {
  char raw_hdr[32];
  int is_ivf = 0;

  if (fread(raw_hdr, 1, 32, input_ctx->file) == 32) {
43
    if (memcmp(IVF_SIGNATURE, raw_hdr, 4) == 0) {
44 45 46 47 48 49 50 51 52 53 54 55
      is_ivf = 1;

      if (mem_get_le16(raw_hdr + 4) != 0) {
        fprintf(stderr, "Error: Unrecognized IVF version! This file may not"
                " 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
56 57
      fix_framerate(&input_ctx->framerate.numerator,
                    &input_ctx->framerate.denominator);
58 59 60
    }
  }

61
  if (!is_ivf) {
62
    rewind(input_ctx->file);
63 64
    input_ctx->detect.buf_read = 0;
  } else {
65
    input_ctx->detect.position = 4;
66
  }
67 68 69
  return is_ivf;
}

70 71
int ivf_read_frame(FILE *infile, uint8_t **buffer,
                   size_t *bytes_read, size_t *buffer_size) {
72 73 74 75 76 77 78 79 80 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
  char raw_header[IVF_FRAME_HDR_SZ] = {0};
  size_t frame_size = 0;

  if (fread(raw_header, IVF_FRAME_HDR_SZ, 1, infile) != 1) {
    if (!feof(infile))
      warn("Failed to read frame size\n");
  } 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;
}
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

struct vpx_video {
  FILE *file;
  unsigned char *buffer;
  size_t buffer_size;
  size_t frame_size;
  unsigned int fourcc;
  int width;
  int height;
};

vpx_video_t *vpx_video_open_file(FILE *file) {
  char raw_hdr[32];
  vpx_video_t *video;

  if (fread(raw_hdr, 1, 32, file) != 32)
    return NULL;  // Can't read file header;

  if (memcmp(IVF_SIGNATURE, raw_hdr, 4) != 0)
    return NULL;  // Wrong IVF signature

  if (mem_get_le16(raw_hdr + 4) != 0)
    return NULL;  // Wrong IVF version

  video = (vpx_video_t *)malloc(sizeof(*video));
  video->file = file;
  video->buffer = NULL;
  video->buffer_size = 0;
  video->frame_size = 0;
  video->fourcc = mem_get_le32(raw_hdr + 8);
  video->width = mem_get_le16(raw_hdr + 12);
  video->height = mem_get_le16(raw_hdr + 14);
  return video;
}

void vpx_video_close(vpx_video_t *video) {
  if (video) {
    free(video->buffer);
    free(video);
  }
}

int vpx_video_get_width(vpx_video_t *video) {
  return video->width;
}

int vpx_video_get_height(vpx_video_t *video) {
  return video->height;
}

unsigned int vpx_video_get_fourcc(vpx_video_t *video) {
  return video->fourcc;
}

int vpx_video_read_frame(vpx_video_t *video) {
  return !ivf_read_frame(video->file, &video->buffer, &video->frame_size,
                         &video->buffer_size);
}

const unsigned char *vpx_video_get_frame(vpx_video_t *video, size_t *size) {
  if (size)
    *size = video->frame_size;

  return video->buffer;
}