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