annotate yuv4mpeg.c @ 260:f3b8a6e60e71 libavformat

PowerPC warnings & optimisations patch by (Dan Christiansen <danchr at daimi dot au dot dk>)
author michaelni
date Sun, 28 Sep 2003 22:53:25 +0000
parents 2efd6fe95fc6
children a313e1080322
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
1 /*
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
2 * YUV4MPEG format
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard.
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
4 *
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
9 *
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
13 * Lesser General Public License for more details.
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
14 *
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
18 */
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
19 #include "avformat.h"
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
20
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
21 #define Y4M_MAGIC "YUV4MPEG2"
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
22 #define Y4M_FRAME_MAGIC "FRAME"
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
23 #define Y4M_LINE_MAX 256
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
24
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
25 static int yuv4_write_header(AVFormatContext *s)
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
26 {
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
27 AVStream *st;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
28 int width, height;
178
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
29 int raten, rated, aspectn, aspectd, n;
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
30 char buf[Y4M_LINE_MAX+1];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
31
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
32 if (s->nb_streams != 1)
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
33 return -EIO;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
34
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
35 st = s->streams[0];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
36 width = st->codec.width;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
37 height = st->codec.height;
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
38
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
39 #if 1
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
40 //this is identical to the code below for exact fps
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
41 av_reduce(&raten, &rated, st->codec.frame_rate, st->codec.frame_rate_base, (1UL<<31)-1);
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
42 #else
178
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
43 {
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
44 int gcd, fps, fps1;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
45
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
46 fps = st->codec.frame_rate;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
47 fps1 = (((float)fps / st->codec.frame_rate_base) * 1000);
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
48
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
49 /* Sorry about this messy code, but mpeg2enc is very picky about
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
50 * the framerates it accepts. */
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
51 switch(fps1) {
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
52 case 23976:
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
53 raten = 24000; /* turn the framerate into a ratio */
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
54 rated = 1001;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
55 break;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
56 case 29970:
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
57 raten = 30000;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
58 rated = 1001;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
59 break;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
60 case 25000:
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
61 raten = 25;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
62 rated = 1;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
63 break;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
64 case 30000:
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
65 raten = 30;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
66 rated = 1;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
67 break;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
68 case 24000:
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
69 raten = 24;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
70 rated = 1;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
71 break;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
72 case 50000:
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
73 raten = 50;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
74 rated = 1;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
75 break;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
76 case 59940:
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
77 raten = 60000;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
78 rated = 1001;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
79 break;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
80 case 60000:
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
81 raten = 60;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
82 rated = 1;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
83 break;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
84 default:
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
85 raten = st->codec.frame_rate; /* this setting should work, but often doesn't */
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
86 rated = st->codec.frame_rate_base;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
87 gcd= av_gcd(raten, rated);
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
88 raten /= gcd;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
89 rated /= gcd;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
90 break;
d1290621cc6a removed warnings
bellard
parents: 85
diff changeset
91 }
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
92 }
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
93 #endif
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
94
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
95 aspectn = 1;
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 65
diff changeset
96 aspectd = 1; /* ffmpeg always uses a 1:1 aspect ratio */ //FIXME not true anymore
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
97
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
98 /* construct stream header, if this is the first frame */
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
99 n = snprintf(buf, sizeof(buf), "%s W%d H%d F%d:%d I%s A%d:%d\n",
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
100 Y4M_MAGIC,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
101 width,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
102 height,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
103 raten, rated,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
104 "p", /* ffmpeg seems to only output progressive video */
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
105 aspectn, aspectd);
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
106 if (n < 0) {
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
107 fprintf(stderr, "Error. YUV4MPEG stream header write failed.\n");
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
108 return -EIO;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
109 } else {
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
110 put_buffer(&s->pb, buf, strlen(buf));
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
111 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
112 return 0;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
113 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
114
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
115 static int yuv4_write_packet(AVFormatContext *s, int stream_index,
241
3d92f793fd67 64 bit pts for writing - more const usage
bellard
parents: 207
diff changeset
116 const uint8_t *buf, int size, int64_t pts)
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
117 {
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
118 AVStream *st = s->streams[stream_index];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
119 ByteIOContext *pb = &s->pb;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
120 AVPicture *picture;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
121 int width, height;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
122 int i, m;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
123 char buf1[20];
65
a58a8a53eb46 * UINTX -> uintx_t INTX -> intx_t
kabi
parents: 18
diff changeset
124 uint8_t *ptr, *ptr1, *ptr2;
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
125
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
126 picture = (AVPicture *)buf;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
127
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
128 /* construct frame header */
256
2efd6fe95fc6 yuv4mpeg.c extra space patch by ("Steven M. Schultz" <sms at 2BSD dot COM>)
michaelni
parents: 241
diff changeset
129 m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
130 put_buffer(pb, buf1, strlen(buf1));
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
131
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
132 width = st->codec.width;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
133 height = st->codec.height;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
134
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
135 ptr = picture->data[0];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
136 for(i=0;i<height;i++) {
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
137 put_buffer(pb, ptr, width);
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
138 ptr += picture->linesize[0];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
139 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
140
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
141 height >>= 1;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
142 width >>= 1;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
143 ptr1 = picture->data[1];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
144 ptr2 = picture->data[2];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
145 for(i=0;i<height;i++) { /* Cb */
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
146 put_buffer(pb, ptr1, width);
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
147 ptr1 += picture->linesize[1];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
148 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
149 for(i=0;i<height;i++) { /* Cr */
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
150 put_buffer(pb, ptr2, width);
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
151 ptr2 += picture->linesize[2];
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
152 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
153 put_flush_packet(pb);
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
154 return 0;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
155 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
156
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
157 static int yuv4_write_trailer(AVFormatContext *s)
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
158 {
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
159 return 0;
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
160 }
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
161
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
162 AVOutputFormat yuv4mpegpipe_oformat = {
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
163 "yuv4mpegpipe",
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
164 "YUV4MPEG pipe format",
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
165 "",
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
166 "yuv4mpeg",
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
167 0,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
168 CODEC_ID_NONE,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
169 CODEC_ID_RAWVIDEO,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
170 yuv4_write_header,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
171 yuv4_write_packet,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
172 yuv4_write_trailer,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
173 .flags = AVFMT_RAWPICTURE,
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
174 };
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
175
207
7865656658dc stdin patch by (Charles Yates <charles dot yates at pandora dot be>)
michaelni
parents: 184
diff changeset
176 /* Header size increased to allow room for optional flags */
7865656658dc stdin patch by (Charles Yates <charles dot yates at pandora dot be>)
michaelni
parents: 184
diff changeset
177 #define MAX_YUV4_HEADER 80
184
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
178 #define MAX_FRAME_HEADER 10
18
01e35bb2689a extracted yuv4mpeg from img.c (untested)
bellard
parents:
diff changeset
179
184
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
180 static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
181 {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
182 char header[MAX_YUV4_HEADER+1];
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
183 int i;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
184 ByteIOContext *pb = &s->pb;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
185 int width, height, raten, rated, aspectn, aspectd;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
186 char lacing;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
187 AVStream *st;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
188
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
189 for (i=0; i<MAX_YUV4_HEADER; i++) {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
190 header[i] = get_byte(pb);
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
191 if (header[i] == '\n') {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
192 header[i+1] = 0;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
193 break;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
194 }
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
195 }
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
196 if (i == MAX_YUV4_HEADER) return -1;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
197 if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
198 sscanf(header+strlen(Y4M_MAGIC), " W%d H%d F%d:%d I%c A%d:%d",
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
199 &width, &height, &raten, &rated, &lacing, &aspectn, &aspectd);
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
200
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
201 st = av_new_stream(s, 0);
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
202 st = s->streams[0];
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
203 st->codec.width = width;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
204 st->codec.height = height;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
205 av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
206 st->codec.frame_rate = raten;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
207 st->codec.frame_rate_base = rated;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
208 st->codec.pix_fmt = PIX_FMT_YUV420P;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
209 st->codec.codec_type = CODEC_TYPE_VIDEO;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
210 st->codec.codec_id = CODEC_ID_RAWVIDEO;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
211
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
212 return 0;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
213 }
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
214
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
215 static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
216 {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
217 int i;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
218 char header[MAX_FRAME_HEADER+1];
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
219 int packet_size, ret, width, height;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
220 AVStream *st = s->streams[0];
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
221
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
222 for (i=0; i<MAX_FRAME_HEADER; i++) {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
223 header[i] = get_byte(&s->pb);
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
224 if (header[i] == '\n') {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
225 header[i+1] = 0;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
226 break;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
227 }
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
228 }
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
229 if (i == MAX_FRAME_HEADER) return -1;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
230 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
231
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
232 width = st->codec.width;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
233 height = st->codec.height;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
234
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
235 packet_size = avpicture_get_size(st->codec.pix_fmt, width, height);
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
236 if (packet_size < 0)
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
237 av_abort();
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
238
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
239 if (av_new_packet(pkt, packet_size) < 0)
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
240 return -EIO;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
241
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
242 pkt->stream_index = 0;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
243 ret = get_buffer(&s->pb, pkt->data, pkt->size);
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
244 if (ret != pkt->size) {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
245 av_free_packet(pkt);
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
246 return -EIO;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
247 } else {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
248 return 0;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
249 }
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
250 }
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
251
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
252 static int yuv4_read_close(AVFormatContext *s)
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
253 {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
254 return 0;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
255 }
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
256
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
257 AVInputFormat yuv4mpegpipe_iformat = {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
258 "yuv4mpegpipe",
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
259 "YUV4MPEG pipe format",
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
260 0,
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
261 NULL,
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
262 yuv4_read_header,
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
263 yuv4_read_packet,
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
264 yuv4_read_close,
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
265 .extensions = "yuv4mpeg"
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
266 };
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
267
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
268 int yuv4mpeg_init(void)
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
269 {
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
270 av_register_input_format(&yuv4mpegpipe_iformat);
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
271 av_register_output_format(&yuv4mpegpipe_oformat);
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
272 return 0;
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
273 }
2438e76dde67 yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents: 178
diff changeset
274