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