]> granicus.if.org Git - libvpx/blob - ivfdec.c
Merge "Optimize vp9_tm_predictor_8x8_neon function"
[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 <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include "./ivfdec.h"
16
17 static const char *IVF_SIGNATURE = "DKIF";
18
19 static void fix_framerate(int *num, int *den) {
20   // Some versions of vpxenc used 1/(2*fps) for the timebase, so
21   // we can guess the framerate using only the timebase in this
22   // case. Other files would require reading ahead to guess the
23   // timebase, like we do for webm.
24   if (*num < 1000) {
25     // Correct for the factor of 2 applied to the timebase in the encoder.
26     if (*num & 1)
27       *den *= 2;
28     else
29       *num /= 2;
30   } else {
31     // Don't know FPS for sure, and don't have readahead code
32     // (yet?), so just default to 30fps.
33     *num = 30;
34     *den = 1;
35   }
36 }
37
38 int file_is_ivf(struct VpxInputContext *input_ctx) {
39   char raw_hdr[32];
40   int is_ivf = 0;
41
42   if (fread(raw_hdr, 1, 32, input_ctx->file) == 32) {
43     if (memcmp(IVF_SIGNATURE, raw_hdr, 4) == 0) {
44       is_ivf = 1;
45
46       if (mem_get_le16(raw_hdr + 4) != 0) {
47         fprintf(stderr, "Error: Unrecognized IVF version! This file may not"
48                 " decode properly.");
49       }
50
51       input_ctx->fourcc = mem_get_le32(raw_hdr + 8);
52       input_ctx->width = mem_get_le16(raw_hdr + 12);
53       input_ctx->height = mem_get_le16(raw_hdr + 14);
54       input_ctx->framerate.numerator = mem_get_le32(raw_hdr + 16);
55       input_ctx->framerate.denominator = mem_get_le32(raw_hdr + 20);
56       fix_framerate(&input_ctx->framerate.numerator,
57                     &input_ctx->framerate.denominator);
58     }
59   }
60
61   if (!is_ivf) {
62     rewind(input_ctx->file);
63     input_ctx->detect.buf_read = 0;
64   } else {
65     input_ctx->detect.position = 4;
66   }
67   return is_ivf;
68 }
69
70 int ivf_read_frame(FILE *infile, uint8_t **buffer,
71                    size_t *bytes_read, size_t *buffer_size) {
72   char raw_header[IVF_FRAME_HDR_SZ] = {0};
73   size_t frame_size = 0;
74
75   if (fread(raw_header, IVF_FRAME_HDR_SZ, 1, infile) != 1) {
76     if (!feof(infile))
77       warn("Failed to read frame size\n");
78   } else {
79     frame_size = mem_get_le32(raw_header);
80
81     if (frame_size > 256 * 1024 * 1024) {
82       warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
83       frame_size = 0;
84     }
85
86     if (frame_size > *buffer_size) {
87       uint8_t *new_buffer = realloc(*buffer, 2 * frame_size);
88
89       if (new_buffer) {
90         *buffer = new_buffer;
91         *buffer_size = 2 * frame_size;
92       } else {
93         warn("Failed to allocate compressed data buffer\n");
94         frame_size = 0;
95       }
96     }
97   }
98
99   if (!feof(infile)) {
100     if (fread(*buffer, 1, frame_size, infile) != frame_size) {
101       warn("Failed to read full frame\n");
102       return 1;
103     }
104
105     *bytes_read = frame_size;
106     return 0;
107   }
108
109   return 1;
110 }
111
112 struct vpx_video {
113   FILE *file;
114   unsigned char *buffer;
115   size_t buffer_size;
116   size_t frame_size;
117   unsigned int fourcc;
118   int width;
119   int height;
120 };
121
122 vpx_video_t *vpx_video_open_file(FILE *file) {
123   char raw_hdr[32];
124   vpx_video_t *video;
125
126   if (fread(raw_hdr, 1, 32, file) != 32)
127     return NULL;  // Can't read file header;
128
129   if (memcmp(IVF_SIGNATURE, raw_hdr, 4) != 0)
130     return NULL;  // Wrong IVF signature
131
132   if (mem_get_le16(raw_hdr + 4) != 0)
133     return NULL;  // Wrong IVF version
134
135   video = (vpx_video_t *)malloc(sizeof(*video));
136   video->file = file;
137   video->buffer = NULL;
138   video->buffer_size = 0;
139   video->frame_size = 0;
140   video->fourcc = mem_get_le32(raw_hdr + 8);
141   video->width = mem_get_le16(raw_hdr + 12);
142   video->height = mem_get_le16(raw_hdr + 14);
143   return video;
144 }
145
146 void vpx_video_close(vpx_video_t *video) {
147   if (video) {
148     free(video->buffer);
149     free(video);
150   }
151 }
152
153 int vpx_video_get_width(vpx_video_t *video) {
154   return video->width;
155 }
156
157 int vpx_video_get_height(vpx_video_t *video) {
158   return video->height;
159 }
160
161 unsigned int vpx_video_get_fourcc(vpx_video_t *video) {
162   return video->fourcc;
163 }
164
165 int vpx_video_read_frame(vpx_video_t *video) {
166   return !ivf_read_frame(video->file, &video->buffer, &video->frame_size,
167                          &video->buffer_size);
168 }
169
170 const unsigned char *vpx_video_get_frame(vpx_video_t *video, size_t *size) {
171   if (size)
172     *size = video->frame_size;
173
174   return video->buffer;
175 }