1 // Y4M file parser by Rik Snel (using yuv4mpeg*.[ch] from
2 // mjpeg.sourceforge.net) (derived from demux_viv.c)
3 // older YUV4MPEG (used by xawtv) support by Alex Beregszaszi
8 #include <string.h> /* strtok */
15 //#include "stream/stream.h"
21 y4m_stream_info_t
* si
;
25 static int y4m_check_file(demuxer_t
* demuxer
){
26 int orig_pos
= stream_tell(demuxer
->stream
);
30 mp_msg(MSGT_DEMUX
, MSGL_V
, "Checking for YUV4MPEG2\n");
32 if(stream_read(demuxer
->stream
, buf
, 9)!=9)
37 if (strncmp("YUV4MPEG2", buf
, 9) && strncmp("YUV4MPEG ", buf
, 9)) {
41 demuxer
->priv
= malloc(sizeof(y4m_priv_t
));
46 if (!strncmp("YUV4MPEG ", buf
, 9))
48 mp_msg(MSGT_DEMUX
, MSGL_V
, "Found older YUV4MPEG format (used by xawtv)\n");
52 mp_msg(MSGT_DEMUX
,MSGL_DBG2
,"Success: YUV4MPEG2\n");
54 stream_seek(demuxer
->stream
, orig_pos
);
56 return DEMUXER_TYPE_Y4M
;
61 // 0 = EOF or no stream found
62 // 1 = successfully read a packet
63 static int demux_y4m_fill_buffer(demuxer_t
*demux
, demux_stream_t
*dsds
) {
64 demux_stream_t
*ds
=demux
->video
;
66 y4m_priv_t
*priv
=demux
->priv
;
68 unsigned char *buf
[3];
71 y4m_init_frame_info(&fi
);
73 demux
->filepos
=stream_tell(demux
->stream
);
75 size
= ((sh_video_t
*)ds
->sh
)->disp_w
*((sh_video_t
*)ds
->sh
)->disp_h
;
77 dp
= new_demux_packet(3*size
/2);
79 /* swap U and V components */
81 buf
[1] = dp
->buffer
+ 5*size
/4;
82 buf
[2] = dp
->buffer
+ size
;
88 c
= stream_read_char(demux
->stream
); /* F */
93 mp_msg(MSGT_DEMUX
, MSGL_V
, "Bad frame at %d\n", (int)stream_tell(demux
->stream
)-1);
96 stream_skip(demux
->stream
, 5); /* RAME\n */
97 stream_read(demux
->stream
, buf
[0], size
);
98 stream_read(demux
->stream
, buf
[1], size
/4);
99 stream_read(demux
->stream
, buf
[2], size
/4);
103 if ((err
=y4m_read_frame(demux
->stream
, priv
->si
, &fi
, buf
)) != Y4M_OK
) {
104 mp_msg(MSGT_DEMUX
, MSGL_V
, "error reading frame %s\n", y4m_strerr(err
));
109 /* This seems to be the right way to calculate the presentation time stamp */
110 dp
->pts
=(float)priv
->framenum
/((sh_video_t
*)ds
->sh
)->fps
;
112 dp
->pos
=demux
->filepos
;
114 ds_add_packet(ds
, dp
);
119 static demuxer_t
* demux_open_y4m(demuxer_t
* demuxer
){
120 y4m_priv_t
* priv
= demuxer
->priv
;
122 sh_video_t
* sh
=new_sh_video(demuxer
,0);
126 priv
->si
= malloc(sizeof(y4m_stream_info_t
));
133 stream_skip(demuxer
->stream
, 8); /* YUV4MPEG */
134 stream_skip(demuxer
->stream
, 1); /* space */
135 stream_read(demuxer
->stream
, (char *)&buf
[0], 3);
137 sh
->disp_w
= atoi(buf
);
138 stream_skip(demuxer
->stream
, 1); /* space */
139 stream_read(demuxer
->stream
, (char *)&buf
[0], 3);
141 sh
->disp_h
= atoi(buf
);
142 stream_skip(demuxer
->stream
, 1); /* space */
143 stream_read(demuxer
->stream
, (char *)&buf
[0], 1);
145 frame_rate_code
= atoi(buf
);
146 stream_skip(demuxer
->stream
, 1); /* new-line */
150 /* values from xawtv */
151 switch(frame_rate_code
)
181 sh
->frametime
= 1.0f
/sh
->fps
;
185 y4m_init_stream_info(priv
->si
);
186 if ((err
=y4m_read_stream_header(demuxer
->stream
, priv
->si
)) != Y4M_OK
)
187 mp_msg(MSGT_DEMUXER
, MSGL_FATAL
, "error parsing YUV4MPEG header: %s\n", y4m_strerr(err
));
190 ratio
= y4m_si_get_framerate(priv
->si
);
192 sh
->fps
=(float)ratio
.n
/(float)ratio
.d
;
196 sh
->frametime
=1.0f
/sh
->fps
;
198 ratio
= y4m_si_get_sampleaspect(priv
->si
);
200 sh
->disp_w
= y4m_si_get_width(priv
->si
);
201 sh
->disp_h
= y4m_si_get_height(priv
->si
);
203 if (ratio
.d
!= 0 && ratio
.n
!= 0)
204 sh
->aspect
= (float)(sh
->disp_w
*ratio
.n
)/(float)(sh
->disp_h
*ratio
.d
);
206 demuxer
->seekable
= 0;
209 sh
->format
= mmioFOURCC('Y', 'V', '1', '2');
211 sh
->bih
=malloc(sizeof(BITMAPINFOHEADER
));
212 memset(sh
->bih
,0,sizeof(BITMAPINFOHEADER
));
214 sh
->bih
->biWidth
= sh
->disp_w
;
215 sh
->bih
->biHeight
= sh
->disp_h
;
217 sh
->bih
->biBitCount
=12;
218 sh
->bih
->biCompression
=sh
->format
;
219 sh
->bih
->biSizeImage
=sh
->bih
->biWidth
*sh
->bih
->biHeight
*3/2; /* YV12 */
221 demuxer
->video
->sh
=sh
;
222 sh
->ds
=demuxer
->video
;
223 demuxer
->video
->id
=0;
226 mp_msg(MSGT_DEMUX
, MSGL_INFO
, "YUV4MPEG2 Video stream %d size: display: %dx%d, codec: %ux%u\n",
227 demuxer
->video
->id
, sh
->disp_w
, sh
->disp_h
, sh
->bih
->biWidth
,
233 static void demux_seek_y4m(demuxer_t
*demuxer
, float rel_seek_secs
, float audio_delay
, int flags
) {
234 sh_video_t
* sh
= demuxer
->video
->sh
;
235 y4m_priv_t
* priv
= demuxer
->priv
;
236 int rel_seek_frames
= sh
->fps
*rel_seek_secs
;
237 int size
= 3*sh
->disp_w
*sh
->disp_h
/2;
238 off_t curr_pos
= stream_tell(demuxer
->stream
);
240 if (priv
->framenum
+ rel_seek_frames
< 0) rel_seek_frames
= -priv
->framenum
;
242 //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));
243 //printf("framenum=%d, curr_pos=%ld, currpos/(size+6)=%f\n", priv->framenum, curr_pos, (float)curr_pos/(float)(size+6));
244 priv
->framenum
+= rel_seek_frames
;
246 if (priv
->is_older
) {
247 /* Well this is easy: every frame takes up size+6 bytes
248 * in the stream and we may assume that the stream pointer
249 * is always at the beginning of a frame.
250 * framenum is the number of the frame that is about to be
251 * demuxed (counting from ONE (see demux_open_y4m)) */
252 stream_seek(demuxer
->stream
, curr_pos
+ rel_seek_frames
*(size
+6));
254 /* should never come here, because seeking for YUV4MPEG2
256 mp_msg(MSGT_DEMUX
, MSGL_WARN
, "Seeking for YUV4MPEG2 not yet implemented!\n");
260 static void demux_close_y4m(demuxer_t
*demuxer
)
262 y4m_priv_t
* priv
= demuxer
->priv
;
267 y4m_fini_stream_info(((y4m_priv_t
*)demuxer
->priv
)->si
);
268 free(((y4m_priv_t
*)demuxer
->priv
)->si
);
274 const demuxer_desc_t demuxer_desc_y4m
= {
281 1, // safe autodetect
283 demux_y4m_fill_buffer
,