annotate libmpdemux/demux_y4m.c @ 6850:5abb9367b128

ogm audio format id is in hex, not dec patch by Moritz Bunkus <moritz@bunkus.org>
author arpi
date Tue, 30 Jul 2002 18:14:02 +0000
parents 8a357300d0ec
children ee7203b48e7c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3786
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
1 // Y4M file parser by Rik Snel (using yuv4mpeg*.[ch] from
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
2 // mjpeg.sourceforge.net) (derived from demux_viv.c)
3789
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
3 // older YUV4MPEG (used by xawtv) support by Alex Beregszaszi
3786
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
4
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
5 #include <stdio.h>
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
6 #include <stdlib.h>
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
7 #include <unistd.h>
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
8 #include <string.h> /* strtok */
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
9
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
10 #include "config.h"
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
11 #include "mp_msg.h"
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
12 #include "help_mp.h"
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
13 #include "yuv4mpeg.h"
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
14
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
15 //#include "stream.h"
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
16 #include "demuxer.h"
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
17 #include "stheader.h"
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
18 #include "bswap.h"
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
19
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
20 typedef struct {
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
21 int framenum;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
22 y4m_stream_info_t* si;
3789
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
23 int is_older;
3786
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
24 } y4m_priv_t;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
25
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
26 int y4m_check_file(demuxer_t* demuxer){
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
27 int orig_pos = stream_tell(demuxer->stream);
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
28 char buf[10];
3789
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
29 y4m_priv_t* priv;
3786
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
30
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
31 mp_msg(MSGT_DEMUX, MSGL_V, "Checking for YUV4MPEG2\n");
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
32
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
33 stream_read(demuxer->stream, buf, 9);
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
34 buf[9] = 0;
3789
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
35
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
36 if (strncmp("YUV4MPEG2", buf, 9) && strncmp("YUV4MPEG ", buf, 9)) {
3786
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
37 mp_msg(MSGT_DEMUX, MSGL_DBG2, "Failed: YUV4MPEG2\n");
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
38 return 0;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
39 }
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
40
3789
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
41 demuxer->priv = malloc(sizeof(y4m_priv_t));
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
42 priv = demuxer->priv;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
43
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
44 priv->is_older = 0;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
45
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
46 if (!strncmp("YUV4MPEG ", buf, 9))
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
47 {
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
48 mp_msg(MSGT_DEMUX, MSGL_V, "Found older YUV4MPEG format (used by xawtv)\n");
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
49 priv->is_older = 1;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
50 }
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
51
3786
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
52 mp_msg(MSGT_DEMUX,MSGL_DBG2,"Success: YUV4MPEG2\n");
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
53
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
54 stream_seek(demuxer->stream, orig_pos);
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
55
3789
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
56 return 1;
3786
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
57 }
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
58
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
59
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
60 // return value:
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
61 // 0 = EOF or no stream found
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
62 // 1 = successfully read a packet
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
63 int demux_y4m_fill_buffer(demuxer_t *demux) {
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
64 demux_stream_t *ds=demux->video;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
65 demux_packet_t *dp;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
66 y4m_priv_t *priv=demux->priv;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
67 y4m_frame_info_t fi;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
68 unsigned char *buf[3];
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
69 int err, size;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
70
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
71 demux->filepos=stream_tell(demux->stream);
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
72
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
73 size = ((sh_video_t*)ds->sh)->disp_w*((sh_video_t*)ds->sh)->disp_h;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
74
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
75 dp = new_demux_packet(3*size/2);
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
76
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
77 /* swap U and V components */
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
78 buf[0] = dp->buffer;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
79 buf[1] = dp->buffer + 5*size/4;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
80 buf[2] = dp->buffer + size;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
81
3789
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
82 if (priv->is_older)
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
83 {
3790
f21832490974 fixed YUV4MPEG frame reader
alex
parents: 3789
diff changeset
84 int c;
f21832490974 fixed YUV4MPEG frame reader
alex
parents: 3789
diff changeset
85
f21832490974 fixed YUV4MPEG frame reader
alex
parents: 3789
diff changeset
86 c = stream_read_char(demux->stream); /* F */
f21832490974 fixed YUV4MPEG frame reader
alex
parents: 3789
diff changeset
87 if (c == -256)
f21832490974 fixed YUV4MPEG frame reader
alex
parents: 3789
diff changeset
88 return 0; /* EOF */
f21832490974 fixed YUV4MPEG frame reader
alex
parents: 3789
diff changeset
89 if (c != 'F')
f21832490974 fixed YUV4MPEG frame reader
alex
parents: 3789
diff changeset
90 {
3863
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
91 mp_msg(MSGT_DEMUX, MSGL_V, "Bad frame at %d\n", (int)stream_tell(demux->stream)-1);
3790
f21832490974 fixed YUV4MPEG frame reader
alex
parents: 3789
diff changeset
92 return 0;
f21832490974 fixed YUV4MPEG frame reader
alex
parents: 3789
diff changeset
93 }
f21832490974 fixed YUV4MPEG frame reader
alex
parents: 3789
diff changeset
94 stream_skip(demux->stream, 5); /* RAME\n */
3789
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
95 stream_read(demux->stream, buf[0], size);
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
96 stream_read(demux->stream, buf[1], size/4);
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
97 stream_read(demux->stream, buf[2], size/4);
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
98 }
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
99 else
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
100 {
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
101 if ((err=y4m_read_frame(demux->stream, priv->si, &fi, buf)) != Y4M_OK) {
3786
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
102 mp_msg(MSGT_DEMUX, MSGL_V, "error reading frame %s\n", y4m_strerr(err));
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
103 return 0;
3789
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
104 }
3786
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
105 }
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
106
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
107 /* This seems to be the right way to calculate the presentation time stamp */
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
108 dp->pts=(float)priv->framenum/((sh_video_t*)ds->sh)->fps;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
109 priv->framenum++;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
110 dp->pos=demux->filepos;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
111 dp->flags=0;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
112 ds_add_packet(ds, dp);
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
113
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
114 return 1;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
115 }
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
116
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
117 void demux_open_y4m(demuxer_t* demuxer){
3789
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
118 y4m_priv_t* priv = demuxer->priv;
3800
3080ef830f93 aspect ratio patch by Rik Snel
alex
parents: 3790
diff changeset
119 y4m_ratio_t ratio;
3786
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
120 sh_video_t* sh=new_sh_video(demuxer,0);
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
121 int err;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
122
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
123 priv->framenum = 0;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
124 priv->si = malloc(sizeof(y4m_stream_info_t));
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
125
3789
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
126 if (priv->is_older)
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
127 {
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
128 char buf[4];
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
129 int frame_rate_code;
3786
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
130
3789
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
131 stream_skip(demuxer->stream, 8); /* YUV4MPEG */
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
132 stream_skip(demuxer->stream, 1); /* space */
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
133 stream_read(demuxer->stream, (char *)&buf[0], 3);
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
134 buf[3] = 0;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
135 sh->disp_w = atoi(buf);
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
136 stream_skip(demuxer->stream, 1); /* space */
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
137 stream_read(demuxer->stream, (char *)&buf[0], 3);
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
138 buf[3] = 0;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
139 sh->disp_h = atoi(buf);
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
140 stream_skip(demuxer->stream, 1); /* space */
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
141 stream_read(demuxer->stream, (char *)&buf[0], 1);
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
142 buf[1] = 0;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
143 frame_rate_code = atoi(buf);
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
144 stream_skip(demuxer->stream, 1); /* new-line */
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
145
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
146 if (!sh->fps)
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
147 {
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
148 /* values from xawtv */
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
149 switch(frame_rate_code)
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
150 {
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
151 case 1:
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
152 sh->fps = 23.976f;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
153 break;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
154 case 2:
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
155 sh->fps = 24.0f;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
156 break;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
157 case 3:
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
158 sh->fps = 25.0f;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
159 break;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
160 case 4:
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
161 sh->fps = 29.97f;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
162 break;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
163 case 5:
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
164 sh->fps = 30.0f;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
165 break;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
166 case 6:
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
167 sh->fps = 50.0f;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
168 break;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
169 case 7:
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
170 sh->fps = 59.94f;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
171 break;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
172 case 8:
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
173 sh->fps = 60.0f;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
174 break;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
175 default:
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
176 sh->fps = 25.0f;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
177 }
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
178 }
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
179 sh->frametime = 1.0f/sh->fps;
3786
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
180 }
3789
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
181 else
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
182 {
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
183 y4m_init_stream_info(priv->si);
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
184 if ((err=y4m_read_stream_header(demuxer->stream, priv->si)) != Y4M_OK)
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
185 mp_msg(MSGT_DEMUXER, MSGL_FATAL, "error parsing YUV4MPEG header: %s\n", y4m_strerr(err));
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
186
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
187 if(!sh->fps) {
3800
3080ef830f93 aspect ratio patch by Rik Snel
alex
parents: 3790
diff changeset
188 ratio = y4m_si_get_framerate(priv->si);
3080ef830f93 aspect ratio patch by Rik Snel
alex
parents: 3790
diff changeset
189 if (ratio.d != 0)
3080ef830f93 aspect ratio patch by Rik Snel
alex
parents: 3790
diff changeset
190 sh->fps=(float)ratio.n/(float)ratio.d;
3789
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
191 else
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
192 sh->fps=15.0f;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
193 }
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
194 sh->frametime=1.0f/sh->fps;
3800
3080ef830f93 aspect ratio patch by Rik Snel
alex
parents: 3790
diff changeset
195
3080ef830f93 aspect ratio patch by Rik Snel
alex
parents: 3790
diff changeset
196 ratio = y4m_si_get_sampleaspect(priv->si);
3080ef830f93 aspect ratio patch by Rik Snel
alex
parents: 3790
diff changeset
197 if (ratio.d != 0 && ratio.n != 0)
3080ef830f93 aspect ratio patch by Rik Snel
alex
parents: 3790
diff changeset
198 sh->aspect = (float)ratio.n/(float)ratio.d;
3786
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
199
3789
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
200 sh->disp_w = y4m_si_get_width(priv->si);
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
201 sh->disp_h = y4m_si_get_height(priv->si);
3863
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
202 demuxer->seekable = 0;
3789
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
203 }
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
204
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
205 sh->format = mmioFOURCC('Y', 'V', '1', '2');
3786
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
206
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
207 sh->bih=malloc(sizeof(BITMAPINFOHEADER));
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
208 memset(sh->bih,0,sizeof(BITMAPINFOHEADER));
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
209 sh->bih->biSize=40;
3789
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
210 sh->bih->biWidth = sh->disp_w;
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
211 sh->bih->biHeight = sh->disp_h;
3786
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
212 sh->bih->biPlanes=3;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
213 sh->bih->biBitCount=12;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
214 sh->bih->biCompression=sh->format;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
215 sh->bih->biSizeImage=sh->bih->biWidth*sh->bih->biHeight*3/2; /* YV12 */
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
216
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
217 demuxer->video->sh=sh;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
218 sh->ds=demuxer->video;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
219 demuxer->video->id=0;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
220
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
221
3863
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
222 mp_msg(MSGT_DEMUX, MSGL_INFO, "YUV4MPEG2 Video stream %d size: display: %dx%d, codec: %ux%u\n",
3786
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
223 demuxer->video->id, sh->disp_w, sh->disp_h, sh->bih->biWidth,
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
224 sh->bih->biHeight);
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
225 }
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
226
3863
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
227 int demux_seek_y4m(demuxer_t *demuxer, float rel_seek_secs, int flags) {
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
228 sh_video_t* sh = demuxer->video->sh;
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
229 y4m_priv_t* priv = demuxer->priv;
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
230 int rel_seek_frames = sh->fps*rel_seek_secs;
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
231 int size = 3*sh->disp_w*sh->disp_h/2;
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
232 off_t curr_pos = stream_tell(demuxer->stream);
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
233
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
234 if (priv->framenum + rel_seek_frames < 0) rel_seek_frames = -priv->framenum;
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
235
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
236 //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));
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
237 //printf("framenum=%d, curr_pos=%ld, currpos/(size+6)=%f\n", priv->framenum, curr_pos, (float)curr_pos/(float)(size+6));
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
238 priv->framenum += rel_seek_frames;
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
239
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
240 if (priv->is_older) {
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
241 /* Well this is easy: every frame takes up size+6 bytes
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
242 * in the stream and we may assume that the stream pointer
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
243 * is always at the beginning of a frame.
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
244 * framenum is the number of the frame that is about to be
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
245 * demuxed (counting from ONE (see demux_open_y4m)) */
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
246 stream_seek(demuxer->stream, curr_pos + rel_seek_frames*(size+6));
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
247 } else {
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
248 /* should never come here, because seeking for YUV4MPEG2
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
249 * is disabled. */
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
250 mp_msg(MSGT_DEMUX, MSGL_WARN, "Seeking for YUV4MPEG2 not yet implemented!\n");
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
251 }
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
252 return 0;
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
253 }
64f5c5749bad Applied Rik Snel's seeking patch
alex
parents: 3800
diff changeset
254
3786
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
255 void demux_close_y4m(demuxer_t *demuxer)
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
256 {
3800
3080ef830f93 aspect ratio patch by Rik Snel
alex
parents: 3790
diff changeset
257 y4m_priv_t* priv = demuxer->priv;
5810
8a357300d0ec Added demuxer uninit
albeu
parents: 3863
diff changeset
258
8a357300d0ec Added demuxer uninit
albeu
parents: 3863
diff changeset
259 if(!priv)
8a357300d0ec Added demuxer uninit
albeu
parents: 3863
diff changeset
260 return;
3789
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
261 if (!priv->is_older)
811b2e1ff030 added support for older YUV4MPEG format (used by xawtv)
alex
parents: 3786
diff changeset
262 y4m_fini_stream_info(((y4m_priv_t*)demuxer->priv)->si);
3786
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
263 free(((y4m_priv_t*)demuxer->priv)->si);
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
264 free(demuxer->priv);
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
265 return;
7ebf504c92d6 yuv4mpeg2 (mjpegtools) support by Rik Snel <rsnel@cube.dyndns.org>
arpi
parents:
diff changeset
266 }