Get rid of completely pointless vt_doit variable
[mplayer/glamo.git] / libmpdemux / demux_y4m.c
blob76eb0adc3576f52c14fd49b23496befde9e1664b
1 /*
2 * Y4M file parser
3 * copyright (c) 2001 Rik Snel
4 * (using yuv4mpeg*.[ch] from mjpeg.sourceforge.net)
5 * (derived from demux_viv.c)
6 * older YUV4MPEG (used by xawtv) support by Alex Beregszaszi
8 * This file is part of MPlayer.
10 * MPlayer is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * MPlayer is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h> /* strtok */
30 #include "config.h"
31 #include "mp_msg.h"
32 #include "help_mp.h"
33 #include "yuv4mpeg.h"
35 //#include "stream/stream.h"
36 #include "demuxer.h"
37 #include "stheader.h"
39 typedef struct {
40 int framenum;
41 y4m_stream_info_t* si;
42 int is_older;
43 } y4m_priv_t;
45 static int y4m_check_file(demuxer_t* demuxer){
46 int orig_pos = stream_tell(demuxer->stream);
47 char buf[10];
48 y4m_priv_t* priv;
50 mp_msg(MSGT_DEMUX, MSGL_V, "Checking for YUV4MPEG2\n");
52 if(stream_read(demuxer->stream, buf, 9)!=9)
53 return 0;
55 buf[9] = 0;
57 if (strncmp("YUV4MPEG2", buf, 9) && strncmp("YUV4MPEG ", buf, 9)) {
58 return 0;
61 demuxer->priv = malloc(sizeof(y4m_priv_t));
62 priv = demuxer->priv;
64 priv->is_older = 0;
66 if (!strncmp("YUV4MPEG ", buf, 9))
68 mp_msg(MSGT_DEMUX, MSGL_V, "Found older YUV4MPEG format (used by xawtv)\n");
69 priv->is_older = 1;
72 mp_msg(MSGT_DEMUX,MSGL_DBG2,"Success: YUV4MPEG2\n");
74 stream_seek(demuxer->stream, orig_pos);
76 return DEMUXER_TYPE_Y4M;
80 // return value:
81 // 0 = EOF or no stream found
82 // 1 = successfully read a packet
83 static int demux_y4m_fill_buffer(demuxer_t *demux, demux_stream_t *dsds) {
84 demux_stream_t *ds=demux->video;
85 demux_packet_t *dp;
86 y4m_priv_t *priv=demux->priv;
87 y4m_frame_info_t fi;
88 unsigned char *buf[3];
89 int err, size;
91 y4m_init_frame_info(&fi);
93 demux->filepos=stream_tell(demux->stream);
95 size = ((sh_video_t*)ds->sh)->disp_w*((sh_video_t*)ds->sh)->disp_h;
97 dp = new_demux_packet(3*size/2);
99 /* swap U and V components */
100 buf[0] = dp->buffer;
101 buf[1] = dp->buffer + 5*size/4;
102 buf[2] = dp->buffer + size;
104 if (priv->is_older)
106 int c;
108 c = stream_read_char(demux->stream); /* F */
109 if (c == -256)
110 return 0; /* EOF */
111 if (c != 'F')
113 mp_msg(MSGT_DEMUX, MSGL_V, "Bad frame at %d\n", (int)stream_tell(demux->stream)-1);
114 return 0;
116 stream_skip(demux->stream, 5); /* RAME\n */
117 stream_read(demux->stream, buf[0], size);
118 stream_read(demux->stream, buf[1], size/4);
119 stream_read(demux->stream, buf[2], size/4);
121 else
123 if ((err=y4m_read_frame(demux->stream, priv->si, &fi, buf)) != Y4M_OK) {
124 mp_msg(MSGT_DEMUX, MSGL_V, "error reading frame %s\n", y4m_strerr(err));
125 return 0;
129 /* This seems to be the right way to calculate the presentation time stamp */
130 dp->pts=(float)priv->framenum/((sh_video_t*)ds->sh)->fps;
131 priv->framenum++;
132 dp->pos=demux->filepos;
133 dp->flags=0;
134 ds_add_packet(ds, dp);
136 return 1;
139 static demuxer_t* demux_open_y4m(demuxer_t* demuxer){
140 y4m_priv_t* priv = demuxer->priv;
141 y4m_ratio_t ratio;
142 sh_video_t* sh=new_sh_video(demuxer,0);
143 int err;
145 priv->framenum = 0;
146 priv->si = malloc(sizeof(y4m_stream_info_t));
148 if (priv->is_older)
150 char buf[4];
151 int frame_rate_code;
153 stream_skip(demuxer->stream, 8); /* YUV4MPEG */
154 stream_skip(demuxer->stream, 1); /* space */
155 stream_read(demuxer->stream, (char *)&buf[0], 3);
156 buf[3] = 0;
157 sh->disp_w = atoi(buf);
158 stream_skip(demuxer->stream, 1); /* space */
159 stream_read(demuxer->stream, (char *)&buf[0], 3);
160 buf[3] = 0;
161 sh->disp_h = atoi(buf);
162 stream_skip(demuxer->stream, 1); /* space */
163 stream_read(demuxer->stream, (char *)&buf[0], 1);
164 buf[1] = 0;
165 frame_rate_code = atoi(buf);
166 stream_skip(demuxer->stream, 1); /* new-line */
168 if (!sh->fps)
170 /* values from xawtv */
171 switch(frame_rate_code)
173 case 1:
174 sh->fps = 23.976f;
175 break;
176 case 2:
177 sh->fps = 24.0f;
178 break;
179 case 3:
180 sh->fps = 25.0f;
181 break;
182 case 4:
183 sh->fps = 29.97f;
184 break;
185 case 5:
186 sh->fps = 30.0f;
187 break;
188 case 6:
189 sh->fps = 50.0f;
190 break;
191 case 7:
192 sh->fps = 59.94f;
193 break;
194 case 8:
195 sh->fps = 60.0f;
196 break;
197 default:
198 sh->fps = 25.0f;
201 sh->frametime = 1.0f/sh->fps;
203 else
205 y4m_init_stream_info(priv->si);
206 if ((err=y4m_read_stream_header(demuxer->stream, priv->si)) != Y4M_OK)
207 mp_msg(MSGT_DEMUXER, MSGL_FATAL, "error parsing YUV4MPEG header: %s\n", y4m_strerr(err));
209 if(!sh->fps) {
210 ratio = y4m_si_get_framerate(priv->si);
211 if (ratio.d != 0)
212 sh->fps=(float)ratio.n/(float)ratio.d;
213 else
214 sh->fps=15.0f;
216 sh->frametime=1.0f/sh->fps;
218 ratio = y4m_si_get_sampleaspect(priv->si);
220 sh->disp_w = y4m_si_get_width(priv->si);
221 sh->disp_h = y4m_si_get_height(priv->si);
223 if (ratio.d != 0 && ratio.n != 0)
224 sh->aspect = (float)(sh->disp_w*ratio.n)/(float)(sh->disp_h*ratio.d);
226 demuxer->seekable = 0;
229 sh->format = mmioFOURCC('Y', 'V', '1', '2');
231 sh->bih=malloc(sizeof(BITMAPINFOHEADER));
232 memset(sh->bih,0,sizeof(BITMAPINFOHEADER));
233 sh->bih->biSize=40;
234 sh->bih->biWidth = sh->disp_w;
235 sh->bih->biHeight = sh->disp_h;
236 sh->bih->biPlanes=3;
237 sh->bih->biBitCount=12;
238 sh->bih->biCompression=sh->format;
239 sh->bih->biSizeImage=sh->bih->biWidth*sh->bih->biHeight*3/2; /* YV12 */
241 demuxer->video->sh=sh;
242 sh->ds=demuxer->video;
243 demuxer->video->id=0;
246 mp_msg(MSGT_DEMUX, MSGL_INFO, "YUV4MPEG2 Video stream %d size: display: %dx%d, codec: %ux%u\n",
247 demuxer->video->id, sh->disp_w, sh->disp_h, sh->bih->biWidth,
248 sh->bih->biHeight);
250 return demuxer;
253 static void demux_seek_y4m(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags) {
254 sh_video_t* sh = demuxer->video->sh;
255 y4m_priv_t* priv = demuxer->priv;
256 int rel_seek_frames = sh->fps*rel_seek_secs;
257 int size = 3*sh->disp_w*sh->disp_h/2;
258 off_t curr_pos = stream_tell(demuxer->stream);
260 if (priv->framenum + rel_seek_frames < 0) rel_seek_frames = -priv->framenum;
262 //printf("seektoframe=%d rel_seek_secs=%f seektooffset=%ld\n", priv->framenum + rel_seek_frames, rel_seek_secs, curr_pos + rel_seek_frames*(size+6));
263 //printf("framenum=%d, curr_pos=%ld, currpos/(size+6)=%f\n", priv->framenum, curr_pos, (float)curr_pos/(float)(size+6));
264 priv->framenum += rel_seek_frames;
266 if (priv->is_older) {
267 /* Well this is easy: every frame takes up size+6 bytes
268 * in the stream and we may assume that the stream pointer
269 * is always at the beginning of a frame.
270 * framenum is the number of the frame that is about to be
271 * demuxed (counting from ONE (see demux_open_y4m)) */
272 stream_seek(demuxer->stream, curr_pos + rel_seek_frames*(size+6));
273 } else {
274 /* should never come here, because seeking for YUV4MPEG2
275 * is disabled. */
276 mp_msg(MSGT_DEMUX, MSGL_WARN, "Seeking for YUV4MPEG2 not yet implemented!\n");
280 static void demux_close_y4m(demuxer_t *demuxer)
282 y4m_priv_t* priv = demuxer->priv;
284 if(!priv)
285 return;
286 if (!priv->is_older)
287 y4m_fini_stream_info(((y4m_priv_t*)demuxer->priv)->si);
288 free(((y4m_priv_t*)demuxer->priv)->si);
289 free(demuxer->priv);
290 return;
294 const demuxer_desc_t demuxer_desc_y4m = {
295 "YUV4MPEG2 demuxer",
296 "y4m",
297 "YUV4MPEG2",
298 "Rik snel",
300 DEMUXER_TYPE_Y4M,
301 1, // safe autodetect
302 y4m_check_file,
303 demux_y4m_fill_buffer,
304 demux_open_y4m,
305 demux_close_y4m,
306 demux_seek_y4m,
307 NULL