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