]> granicus.if.org Git - libvpx/blob - webmdec.c
Merge "Improve vp9_fdct4x4_sse2 (x1.2)"
[libvpx] / webmdec.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 "./webmdec.h"
12
13 #include <stdarg.h>
14
15 #include "nestegg/include/nestegg/nestegg.h"
16
17 static int nestegg_read_cb(void *buffer, size_t length, void *userdata) {
18   FILE *f = userdata;
19
20   if (fread(buffer, 1, length, f) < length) {
21     if (ferror(f))
22       return -1;
23     if (feof(f))
24       return 0;
25   }
26   return 1;
27 }
28
29 static int nestegg_seek_cb(int64_t offset, int whence, void *userdata) {
30   switch (whence) {
31     case NESTEGG_SEEK_SET:
32       whence = SEEK_SET;
33       break;
34     case NESTEGG_SEEK_CUR:
35       whence = SEEK_CUR;
36       break;
37     case NESTEGG_SEEK_END:
38       whence = SEEK_END;
39       break;
40   };
41   return fseek(userdata, (int32_t)offset, whence) ? -1 : 0;
42 }
43
44 static int64_t nestegg_tell_cb(void *userdata) {
45   return ftell(userdata);
46 }
47
48 static void nestegg_log_cb(nestegg *context,
49                            unsigned int severity,
50                            char const *format, ...) {
51   va_list ap;
52   va_start(ap, format);
53   vfprintf(stderr, format, ap);
54   fprintf(stderr, "\n");
55   va_end(ap);
56 }
57
58 int file_is_webm(struct WebmInputContext *webm_ctx,
59                  struct VpxInputContext *vpx_ctx) {
60   uint32_t i, n;
61   int track_type = -1;
62   int codec_id;
63
64   nestegg_io io = {nestegg_read_cb, nestegg_seek_cb, nestegg_tell_cb, 0};
65   nestegg_video_params params;
66
67   io.userdata = vpx_ctx->file;
68   if (nestegg_init(&webm_ctx->nestegg_ctx, io, NULL))
69     goto fail;
70
71   if (nestegg_track_count(webm_ctx->nestegg_ctx, &n))
72     goto fail;
73
74   for (i = 0; i < n; i++) {
75     track_type = nestegg_track_type(webm_ctx->nestegg_ctx, i);
76
77     if (track_type == NESTEGG_TRACK_VIDEO)
78       break;
79     else if (track_type < 0)
80       goto fail;
81   }
82
83   codec_id = nestegg_track_codec_id(webm_ctx->nestegg_ctx, i);
84   if (codec_id == NESTEGG_CODEC_VP8) {
85     vpx_ctx->fourcc = VP8_FOURCC_MASK;
86   } else if (codec_id == NESTEGG_CODEC_VP9) {
87     vpx_ctx->fourcc = VP9_FOURCC_MASK;
88   } else {
89     fatal("Not VPx video, quitting.\n");
90   }
91
92   webm_ctx->video_track = i;
93
94   if (nestegg_track_video_params(webm_ctx->nestegg_ctx, i, &params))
95     goto fail;
96
97   vpx_ctx->framerate.denominator = 0;
98   vpx_ctx->framerate.numerator = 0;
99   vpx_ctx->width = params.width;
100   vpx_ctx->height = params.height;
101
102   return 1;
103
104  fail:
105   webm_ctx->nestegg_ctx = NULL;
106   rewind(vpx_ctx->file);
107
108   return 0;
109 }
110
111 int webm_read_frame(struct WebmInputContext *webm_ctx,
112                     uint8_t **buffer,
113                     size_t *bytes_in_buffer,
114                     size_t *buffer_size) {
115   if (webm_ctx->chunk >= webm_ctx->chunks) {
116     uint32_t track;
117
118     do {
119       /* End of this packet, get another. */
120       if (webm_ctx->pkt)
121         nestegg_free_packet(webm_ctx->pkt);
122
123       if (nestegg_read_packet(webm_ctx->nestegg_ctx, &webm_ctx->pkt) <= 0 ||
124           nestegg_packet_track(webm_ctx->pkt, &track)) {
125         return 1;
126       }
127     } while (track != webm_ctx->video_track);
128
129     if (nestegg_packet_count(webm_ctx->pkt, &webm_ctx->chunks))
130       return 1;
131
132     webm_ctx->chunk = 0;
133   }
134
135   if (nestegg_packet_data(webm_ctx->pkt, webm_ctx->chunk,
136                           buffer, bytes_in_buffer)) {
137     return 1;
138   }
139
140   webm_ctx->chunk++;
141   return 0;
142 }
143
144 int webm_guess_framerate(struct WebmInputContext *webm_ctx,
145                          struct VpxInputContext *vpx_ctx) {
146   uint32_t i;
147   uint64_t tstamp = 0;
148
149   /* Check to see if we can seek before we parse any data. */
150   if (nestegg_track_seek(webm_ctx->nestegg_ctx, webm_ctx->video_track, 0)) {
151     warn("Failed to guess framerate (no Cues), set to 30fps.\n");
152     vpx_ctx->framerate.numerator = 30;
153     vpx_ctx->framerate.denominator  = 1;
154     return 0;
155   }
156
157   /* Guess the framerate. Read up to 1 second, or 50 video packets,
158    * whichever comes first.
159    */
160   for (i = 0; tstamp < 1000000000 && i < 50;) {
161     nestegg_packet *pkt;
162     uint32_t track;
163
164     if (nestegg_read_packet(webm_ctx->nestegg_ctx, &pkt) <= 0)
165       break;
166
167     nestegg_packet_track(pkt, &track);
168     if (track == webm_ctx->video_track) {
169       nestegg_packet_tstamp(pkt, &tstamp);
170       ++i;
171     }
172
173     nestegg_free_packet(pkt);
174   }
175
176   if (nestegg_track_seek(webm_ctx->nestegg_ctx, webm_ctx->video_track, 0))
177     goto fail;
178
179   vpx_ctx->framerate.numerator = (i - 1) * 1000000;
180   vpx_ctx->framerate.denominator = (int)(tstamp / 1000);
181   return 0;
182
183  fail:
184   nestegg_destroy(webm_ctx->nestegg_ctx);
185   webm_ctx->nestegg_ctx = NULL;
186   rewind(vpx_ctx->file);
187   return 1;
188 }
189
190 void webm_free(struct WebmInputContext *webm_ctx) {
191   if (webm_ctx && webm_ctx->nestegg_ctx)
192     nestegg_destroy(webm_ctx->nestegg_ctx);
193 }