Mercurial > libavformat.hg
annotate yuv4mpeg.c @ 922:7f20b3ce1100 libavformat
dynamically increase probe buffer until format is detected
author | michael |
---|---|
date | Thu, 02 Feb 2006 11:27:35 +0000 |
parents | edbe5c3717f9 |
children | 8973dbae81e8 |
rev | line source |
---|---|
18 | 1 /* |
2 * YUV4MPEG format | |
3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard. | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
887
diff
changeset
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
18 | 18 */ |
19 #include "avformat.h" | |
20 | |
21 #define Y4M_MAGIC "YUV4MPEG2" | |
22 #define Y4M_FRAME_MAGIC "FRAME" | |
23 #define Y4M_LINE_MAX 256 | |
24 | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
838
diff
changeset
|
25 #ifdef CONFIG_MUXERS |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
26 |
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
27 static int yuv4_generate_header(AVFormatContext *s, char* buf) |
18 | 28 { |
29 AVStream *st; | |
30 int width, height; | |
288 | 31 int raten, rated, aspectn, aspectd, n; |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
32 char inter; |
634 | 33 char *colorspace = ""; |
18 | 34 |
35 st = s->streams[0]; | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
36 width = st->codec->width; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
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 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
39 av_reduce(&raten, &rated, st->codec->time_base.den, st->codec->time_base.num, (1UL<<31)-1); |
885 | 40 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
41 aspectn = st->codec->sample_aspect_ratio.num; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
42 aspectd = st->codec->sample_aspect_ratio.den; |
885 | 43 |
634 | 44 if ( aspectn == 0 && aspectd == 1 ) aspectd = 0; // 0:0 means unknown |
45 | |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
46 inter = 'p'; /* progressive is the default */ |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
47 if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame) { |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
48 inter = st->codec->coded_frame->top_field_first ? 't' : 'b'; |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
49 } |
18 | 50 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
51 switch(st->codec->pix_fmt) { |
648
340f1911cd54
add luma only support to yuv4mpeg patch by (Roine Gustafsson <roine users.sourceforge net>)
michael
parents:
634
diff
changeset
|
52 case PIX_FMT_GRAY8: |
340f1911cd54
add luma only support to yuv4mpeg patch by (Roine Gustafsson <roine users.sourceforge net>)
michael
parents:
634
diff
changeset
|
53 colorspace = " Cmono"; |
340f1911cd54
add luma only support to yuv4mpeg patch by (Roine Gustafsson <roine users.sourceforge net>)
michael
parents:
634
diff
changeset
|
54 break; |
634 | 55 case PIX_FMT_YUV411P: |
56 colorspace = " C411 XYSCSS=411"; | |
57 break; | |
58 case PIX_FMT_YUV420P: | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
59 colorspace = (st->codec->codec_id == CODEC_ID_DVVIDEO)?" C420paldv XYSCSS=420PALDV":" C420mpeg2 XYSCSS=420MPEG2"; |
634 | 60 break; |
61 case PIX_FMT_YUV422P: | |
62 colorspace = " C422 XYSCSS=422"; | |
63 break; | |
64 case PIX_FMT_YUV444P: | |
65 colorspace = " C444 XYSCSS=444"; | |
66 break; | |
67 } | |
68 | |
18 | 69 /* construct stream header, if this is the first frame */ |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
70 n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n", |
18 | 71 Y4M_MAGIC, |
72 width, | |
73 height, | |
74 raten, rated, | |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
75 inter, |
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
76 aspectn, aspectd, |
634 | 77 colorspace); |
885 | 78 |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
79 return n; |
18 | 80 } |
81 | |
468 | 82 static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt) |
18 | 83 { |
468 | 84 AVStream *st = s->streams[pkt->stream_index]; |
18 | 85 ByteIOContext *pb = &s->pb; |
86 AVPicture *picture; | |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
87 int* first_pkt = s->priv_data; |
634 | 88 int width, height, h_chroma_shift, v_chroma_shift; |
18 | 89 int i, m; |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
90 char buf2[Y4M_LINE_MAX+1]; |
18 | 91 char buf1[20]; |
65 | 92 uint8_t *ptr, *ptr1, *ptr2; |
18 | 93 |
468 | 94 picture = (AVPicture *)pkt->data; |
18 | 95 |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
96 /* for the first packet we have to output the header as well */ |
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
97 if (*first_pkt) { |
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
98 *first_pkt = 0; |
887 | 99 if (yuv4_generate_header(s, buf2) < 0) { |
100 av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n"); | |
101 return AVERROR_IO; | |
102 } else { | |
103 put_buffer(pb, buf2, strlen(buf2)); | |
104 } | |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
105 } |
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
106 |
18 | 107 /* construct frame header */ |
885 | 108 |
256
2efd6fe95fc6
yuv4mpeg.c extra space patch by ("Steven M. Schultz" <sms at 2BSD dot COM>)
michaelni
parents:
241
diff
changeset
|
109 m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC); |
18 | 110 put_buffer(pb, buf1, strlen(buf1)); |
111 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
112 width = st->codec->width; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
113 height = st->codec->height; |
885 | 114 |
18 | 115 ptr = picture->data[0]; |
116 for(i=0;i<height;i++) { | |
117 put_buffer(pb, ptr, width); | |
118 ptr += picture->linesize[0]; | |
119 } | |
120 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
121 if (st->codec->pix_fmt != PIX_FMT_GRAY8){ |
634 | 122 // Adjust for smaller Cb and Cr planes |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
123 avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift, &v_chroma_shift); |
634 | 124 width >>= h_chroma_shift; |
125 height >>= v_chroma_shift; | |
126 | |
18 | 127 ptr1 = picture->data[1]; |
128 ptr2 = picture->data[2]; | |
887 | 129 for(i=0;i<height;i++) { /* Cb */ |
18 | 130 put_buffer(pb, ptr1, width); |
131 ptr1 += picture->linesize[1]; | |
132 } | |
887 | 133 for(i=0;i<height;i++) { /* Cr */ |
18 | 134 put_buffer(pb, ptr2, width); |
135 ptr2 += picture->linesize[2]; | |
136 } | |
648
340f1911cd54
add luma only support to yuv4mpeg patch by (Roine Gustafsson <roine users.sourceforge net>)
michael
parents:
634
diff
changeset
|
137 } |
18 | 138 put_flush_packet(pb); |
139 return 0; | |
140 } | |
141 | |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
142 static int yuv4_write_header(AVFormatContext *s) |
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
143 { |
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
144 int* first_pkt = s->priv_data; |
885 | 145 |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
146 if (s->nb_streams != 1) |
482 | 147 return AVERROR_IO; |
885 | 148 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
149 if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) { |
634 | 150 av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n"); |
885 | 151 } |
152 else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) && | |
153 (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) && | |
154 (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) && | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
155 (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) { |
648
340f1911cd54
add luma only support to yuv4mpeg patch by (Roine Gustafsson <roine users.sourceforge net>)
michael
parents:
634
diff
changeset
|
156 av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, yuv422p, yuv420p, yuv411p and gray pixel formats. Use -pix_fmt to select one.\n"); |
887 | 157 return AVERROR_IO; |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
158 } |
885 | 159 |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
160 *first_pkt = 1; |
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
161 return 0; |
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
162 } |
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
163 |
18 | 164 static int yuv4_write_trailer(AVFormatContext *s) |
165 { | |
166 return 0; | |
167 } | |
168 | |
169 AVOutputFormat yuv4mpegpipe_oformat = { | |
170 "yuv4mpegpipe", | |
171 "YUV4MPEG pipe format", | |
172 "", | |
739
db0a5e0f4db5
Adds read probe to y4m, and changes the extension to .y4m patch by (Roine Gustafsson <roine users sourceforge net)
michael
parents:
738
diff
changeset
|
173 "y4m", |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
174 sizeof(int), |
18 | 175 CODEC_ID_NONE, |
176 CODEC_ID_RAWVIDEO, | |
177 yuv4_write_header, | |
178 yuv4_write_packet, | |
179 yuv4_write_trailer, | |
180 .flags = AVFMT_RAWPICTURE, | |
181 }; | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
838
diff
changeset
|
182 #endif //CONFIG_MUXERS |
18 | 183 |
207
7865656658dc
stdin patch by (Charles Yates <charles dot yates at pandora dot be>)
michaelni
parents:
184
diff
changeset
|
184 /* 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
|
185 #define MAX_YUV4_HEADER 80 |
634 | 186 #define MAX_FRAME_HEADER 80 |
18 | 187 |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
188 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
|
189 { |
634 | 190 char header[MAX_YUV4_HEADER+10]; // Include headroom for the longest option |
191 char *tokstart,*tokend,*header_end; | |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
192 int i; |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
193 ByteIOContext *pb = &s->pb; |
634 | 194 int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0,interlaced_frame=0,top_field_first=0; |
738
dad78387544a
Update yuv4mpeg to use PIX_FMT_NONE patch by (Roine Gustafsson <roine users sourceforge net)
michael
parents:
648
diff
changeset
|
195 enum PixelFormat pix_fmt=PIX_FMT_NONE,alt_pix_fmt=PIX_FMT_NONE; |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
196 AVStream *st; |
885 | 197 |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
198 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
|
199 header[i] = get_byte(pb); |
887 | 200 if (header[i] == '\n') { |
201 header[i+1] = 0x20; // Add a space after last option. Makes parsing "444" vs "444alpha" easier. | |
202 header[i+2] = 0; | |
203 break; | |
204 } | |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
205 } |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
206 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
|
207 if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1; |
634 | 208 |
209 header_end = &header[i+1]; // Include space | |
210 for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) { | |
211 if (*tokstart==0x20) continue; | |
212 switch (*tokstart++) { | |
213 case 'W': // Width. Required. | |
214 width = strtol(tokstart, &tokend, 10); | |
215 tokstart=tokend; | |
216 break; | |
217 case 'H': // Height. Required. | |
218 height = strtol(tokstart, &tokend, 10); | |
219 tokstart=tokend; | |
220 break; | |
221 case 'C': // Color space | |
222 if (strncmp("420jpeg",tokstart,7)==0) | |
223 pix_fmt = PIX_FMT_YUV420P; | |
224 else if (strncmp("420mpeg2",tokstart,8)==0) | |
225 pix_fmt = PIX_FMT_YUV420P; | |
226 else if (strncmp("420paldv", tokstart, 8)==0) | |
227 pix_fmt = PIX_FMT_YUV420P; | |
228 else if (strncmp("411", tokstart, 3)==0) | |
229 pix_fmt = PIX_FMT_YUV411P; | |
230 else if (strncmp("422", tokstart, 3)==0) | |
231 pix_fmt = PIX_FMT_YUV422P; | |
232 else if (strncmp("444alpha", tokstart, 8)==0) { | |
233 av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n"); | |
234 return -1; | |
235 } else if (strncmp("444", tokstart, 3)==0) | |
236 pix_fmt = PIX_FMT_YUV444P; | |
237 else if (strncmp("mono",tokstart, 4)==0) { | |
648
340f1911cd54
add luma only support to yuv4mpeg patch by (Roine Gustafsson <roine users.sourceforge net>)
michael
parents:
634
diff
changeset
|
238 pix_fmt = PIX_FMT_GRAY8; |
634 | 239 } else { |
240 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n"); | |
241 return -1; | |
242 } | |
243 while(tokstart<header_end&&*tokstart!=0x20) tokstart++; | |
244 break; | |
245 case 'I': // Interlace type | |
246 switch (*tokstart++){ | |
247 case '?': | |
248 break; | |
249 case 'p': | |
250 interlaced_frame=0; | |
251 break; | |
252 case 't': | |
253 interlaced_frame=1; | |
254 top_field_first=1; | |
255 break; | |
256 case 'b': | |
257 interlaced_frame=1; | |
258 top_field_first=0; | |
259 break; | |
260 case 'm': | |
261 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n"); | |
262 return -1; | |
263 default: | |
264 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n"); | |
265 return -1; | |
266 } | |
267 break; | |
268 case 'F': // Frame rate | |
269 sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown | |
270 while(tokstart<header_end&&*tokstart!=0x20) tokstart++; | |
271 break; | |
272 case 'A': // Pixel aspect | |
273 sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown | |
274 while(tokstart<header_end&&*tokstart!=0x20) tokstart++; | |
275 break; | |
276 case 'X': // Vendor extensions | |
277 if (strncmp("YSCSS=",tokstart,6)==0) { | |
278 // Older nonstandard pixel format representation | |
279 tokstart+=6; | |
280 if (strncmp("420JPEG",tokstart,7)==0) | |
281 alt_pix_fmt=PIX_FMT_YUV420P; | |
282 else if (strncmp("420MPEG2",tokstart,8)==0) | |
283 alt_pix_fmt=PIX_FMT_YUV420P; | |
284 else if (strncmp("420PALDV",tokstart,8)==0) | |
285 alt_pix_fmt=PIX_FMT_YUV420P; | |
286 else if (strncmp("411",tokstart,3)==0) | |
287 alt_pix_fmt=PIX_FMT_YUV411P; | |
288 else if (strncmp("422",tokstart,3)==0) | |
289 alt_pix_fmt=PIX_FMT_YUV422P; | |
290 else if (strncmp("444",tokstart,3)==0) | |
291 alt_pix_fmt=PIX_FMT_YUV444P; | |
292 } | |
293 while(tokstart<header_end&&*tokstart!=0x20) tokstart++; | |
294 break; | |
295 } | |
885 | 296 } |
634 | 297 |
298 if ((width == -1) || (height == -1)) { | |
299 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n"); | |
885 | 300 return -1; |
634 | 301 } |
885 | 302 |
738
dad78387544a
Update yuv4mpeg to use PIX_FMT_NONE patch by (Roine Gustafsson <roine users sourceforge net)
michael
parents:
648
diff
changeset
|
303 if (pix_fmt == PIX_FMT_NONE) { |
dad78387544a
Update yuv4mpeg to use PIX_FMT_NONE patch by (Roine Gustafsson <roine users sourceforge net)
michael
parents:
648
diff
changeset
|
304 if (alt_pix_fmt == PIX_FMT_NONE) |
634 | 305 pix_fmt = PIX_FMT_YUV420P; |
306 else | |
307 pix_fmt = alt_pix_fmt; | |
308 } | |
309 | |
310 if (raten == 0 && rated == 0) { | |
311 // Frame rate unknown | |
312 raten = 25; | |
313 rated = 1; | |
314 } | |
315 | |
316 if (aspectn == 0 && aspectd == 0) { | |
317 // Pixel aspect unknown | |
318 aspectd = 1; | |
319 } | |
885 | 320 |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
321 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
|
322 st = s->streams[0]; |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
323 st->codec->width = width; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
324 st->codec->height = height; |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
325 av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1); |
743 | 326 av_set_pts_info(st, 64, rated, raten); |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
327 st->codec->pix_fmt = pix_fmt; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
328 st->codec->codec_type = CODEC_TYPE_VIDEO; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
329 st->codec->codec_id = CODEC_ID_RAWVIDEO; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
330 st->codec->sample_aspect_ratio= (AVRational){aspectn, aspectd}; |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
331 |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
332 return 0; |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
333 } |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
334 |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
335 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
|
336 { |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
337 int i; |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
338 char header[MAX_FRAME_HEADER+1]; |
838 | 339 int packet_size, width, height; |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
340 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
|
341 |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
342 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
|
343 header[i] = get_byte(&s->pb); |
887 | 344 if (header[i] == '\n') { |
345 header[i+1] = 0; | |
346 break; | |
347 } | |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
348 } |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
349 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
|
350 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1; |
885 | 351 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
352 width = st->codec->width; |
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
353 height = st->codec->height; |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
354 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
355 packet_size = avpicture_get_size(st->codec->pix_fmt, width, height); |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
356 if (packet_size < 0) |
537 | 357 return -1; |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
358 |
775 | 359 if (av_get_packet(&s->pb, pkt, packet_size) != packet_size) |
482 | 360 return AVERROR_IO; |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
361 |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
362 pkt->stream_index = 0; |
775 | 363 return 0; |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
364 } |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
365 |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
366 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
|
367 { |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
368 return 0; |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
369 } |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
370 |
739
db0a5e0f4db5
Adds read probe to y4m, and changes the extension to .y4m patch by (Roine Gustafsson <roine users sourceforge net)
michael
parents:
738
diff
changeset
|
371 static int yuv4_probe(AVProbeData *pd) |
db0a5e0f4db5
Adds read probe to y4m, and changes the extension to .y4m patch by (Roine Gustafsson <roine users sourceforge net)
michael
parents:
738
diff
changeset
|
372 { |
db0a5e0f4db5
Adds read probe to y4m, and changes the extension to .y4m patch by (Roine Gustafsson <roine users sourceforge net)
michael
parents:
738
diff
changeset
|
373 /* check file header */ |
db0a5e0f4db5
Adds read probe to y4m, and changes the extension to .y4m patch by (Roine Gustafsson <roine users sourceforge net)
michael
parents:
738
diff
changeset
|
374 if (pd->buf_size <= sizeof(Y4M_MAGIC)) |
db0a5e0f4db5
Adds read probe to y4m, and changes the extension to .y4m patch by (Roine Gustafsson <roine users sourceforge net)
michael
parents:
738
diff
changeset
|
375 return 0; |
db0a5e0f4db5
Adds read probe to y4m, and changes the extension to .y4m patch by (Roine Gustafsson <roine users sourceforge net)
michael
parents:
738
diff
changeset
|
376 if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1)==0) |
db0a5e0f4db5
Adds read probe to y4m, and changes the extension to .y4m patch by (Roine Gustafsson <roine users sourceforge net)
michael
parents:
738
diff
changeset
|
377 return AVPROBE_SCORE_MAX; |
db0a5e0f4db5
Adds read probe to y4m, and changes the extension to .y4m patch by (Roine Gustafsson <roine users sourceforge net)
michael
parents:
738
diff
changeset
|
378 else |
db0a5e0f4db5
Adds read probe to y4m, and changes the extension to .y4m patch by (Roine Gustafsson <roine users sourceforge net)
michael
parents:
738
diff
changeset
|
379 return 0; |
db0a5e0f4db5
Adds read probe to y4m, and changes the extension to .y4m patch by (Roine Gustafsson <roine users sourceforge net)
michael
parents:
738
diff
changeset
|
380 } |
db0a5e0f4db5
Adds read probe to y4m, and changes the extension to .y4m patch by (Roine Gustafsson <roine users sourceforge net)
michael
parents:
738
diff
changeset
|
381 |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
382 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
|
383 "yuv4mpegpipe", |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
384 "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
|
385 0, |
739
db0a5e0f4db5
Adds read probe to y4m, and changes the extension to .y4m patch by (Roine Gustafsson <roine users sourceforge net)
michael
parents:
738
diff
changeset
|
386 yuv4_probe, |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
387 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
|
388 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
|
389 yuv4_read_close, |
739
db0a5e0f4db5
Adds read probe to y4m, and changes the extension to .y4m patch by (Roine Gustafsson <roine users sourceforge net)
michael
parents:
738
diff
changeset
|
390 .extensions = "y4m" |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
391 }; |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
392 |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
393 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
|
394 { |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
395 av_register_input_format(&yuv4mpegpipe_iformat); |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
838
diff
changeset
|
396 #ifdef CONFIG_MUXERS |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
397 av_register_output_format(&yuv4mpegpipe_oformat); |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
838
diff
changeset
|
398 #endif //CONFIG_MUXERS |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
399 return 0; |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
400 } |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
401 |