Mercurial > libavformat.hg
annotate yuv4mpeg.c @ 1316:d584d375f928 libavformat
Add support for 8-bit grayscale to V4L grabber
patch by Michael Benjamin % neuroptik AH gmail.com %
Original thread:
Date: Aug 24, 2006 5:40 PM
Subject: [Ffmpeg-devel] V4L grab.c patch for Grayscale
author | gpoirier |
---|---|
date | Fri, 15 Sep 2006 11:17:41 +0000 |
parents | d18cc9a1fd02 |
children | 0899bfe4105c |
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 | |
934
8973dbae81e8
Correctly set the interlaced_frame and top_field_first fields.
diego
parents:
896
diff
changeset
|
25 struct frame_attributes { |
8973dbae81e8
Correctly set the interlaced_frame and top_field_first fields.
diego
parents:
896
diff
changeset
|
26 int interlaced_frame; |
8973dbae81e8
Correctly set the interlaced_frame and top_field_first fields.
diego
parents:
896
diff
changeset
|
27 int top_field_first; |
8973dbae81e8
Correctly set the interlaced_frame and top_field_first fields.
diego
parents:
896
diff
changeset
|
28 }; |
8973dbae81e8
Correctly set the interlaced_frame and top_field_first fields.
diego
parents:
896
diff
changeset
|
29 |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
30 static int yuv4_generate_header(AVFormatContext *s, char* buf) |
18 | 31 { |
32 AVStream *st; | |
33 int width, height; | |
288 | 34 int raten, rated, aspectn, aspectd, n; |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
35 char inter; |
1123
6992dd78ff68
Add (mostly) const to variable and parameter declaration, where a char* was
diego
parents:
935
diff
changeset
|
36 const char *colorspace = ""; |
18 | 37 |
38 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
|
39 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
|
40 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
|
41 |
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 av_reduce(&raten, &rated, st->codec->time_base.den, st->codec->time_base.num, (1UL<<31)-1); |
885 | 43 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
44 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
|
45 aspectd = st->codec->sample_aspect_ratio.den; |
885 | 46 |
634 | 47 if ( aspectn == 0 && aspectd == 1 ) aspectd = 0; // 0:0 means unknown |
48 | |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
49 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
|
50 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
|
51 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
|
52 } |
18 | 53 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
54 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
|
55 case PIX_FMT_GRAY8: |
340f1911cd54
add luma only support to yuv4mpeg patch by (Roine Gustafsson <roine users.sourceforge net>)
michael
parents:
634
diff
changeset
|
56 colorspace = " Cmono"; |
340f1911cd54
add luma only support to yuv4mpeg patch by (Roine Gustafsson <roine users.sourceforge net>)
michael
parents:
634
diff
changeset
|
57 break; |
634 | 58 case PIX_FMT_YUV411P: |
59 colorspace = " C411 XYSCSS=411"; | |
60 break; | |
61 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
|
62 colorspace = (st->codec->codec_id == CODEC_ID_DVVIDEO)?" C420paldv XYSCSS=420PALDV":" C420mpeg2 XYSCSS=420MPEG2"; |
634 | 63 break; |
64 case PIX_FMT_YUV422P: | |
65 colorspace = " C422 XYSCSS=422"; | |
66 break; | |
67 case PIX_FMT_YUV444P: | |
68 colorspace = " C444 XYSCSS=444"; | |
69 break; | |
70 } | |
71 | |
18 | 72 /* 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
|
73 n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n", |
18 | 74 Y4M_MAGIC, |
75 width, | |
76 height, | |
77 raten, rated, | |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
78 inter, |
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
79 aspectn, aspectd, |
634 | 80 colorspace); |
885 | 81 |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
82 return n; |
18 | 83 } |
84 | |
468 | 85 static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt) |
18 | 86 { |
468 | 87 AVStream *st = s->streams[pkt->stream_index]; |
18 | 88 ByteIOContext *pb = &s->pb; |
89 AVPicture *picture; | |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
90 int* first_pkt = s->priv_data; |
634 | 91 int width, height, h_chroma_shift, v_chroma_shift; |
18 | 92 int i, m; |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
93 char buf2[Y4M_LINE_MAX+1]; |
18 | 94 char buf1[20]; |
65 | 95 uint8_t *ptr, *ptr1, *ptr2; |
18 | 96 |
468 | 97 picture = (AVPicture *)pkt->data; |
18 | 98 |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
99 /* 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
|
100 if (*first_pkt) { |
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
101 *first_pkt = 0; |
887 | 102 if (yuv4_generate_header(s, buf2) < 0) { |
103 av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n"); | |
104 return AVERROR_IO; | |
105 } else { | |
106 put_buffer(pb, buf2, strlen(buf2)); | |
107 } | |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
108 } |
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
109 |
18 | 110 /* construct frame header */ |
885 | 111 |
256
2efd6fe95fc6
yuv4mpeg.c extra space patch by ("Steven M. Schultz" <sms at 2BSD dot COM>)
michaelni
parents:
241
diff
changeset
|
112 m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC); |
18 | 113 put_buffer(pb, buf1, strlen(buf1)); |
114 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
115 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
|
116 height = st->codec->height; |
885 | 117 |
18 | 118 ptr = picture->data[0]; |
119 for(i=0;i<height;i++) { | |
120 put_buffer(pb, ptr, width); | |
121 ptr += picture->linesize[0]; | |
122 } | |
123 | |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
124 if (st->codec->pix_fmt != PIX_FMT_GRAY8){ |
634 | 125 // 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
|
126 avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift, &v_chroma_shift); |
634 | 127 width >>= h_chroma_shift; |
128 height >>= v_chroma_shift; | |
129 | |
18 | 130 ptr1 = picture->data[1]; |
131 ptr2 = picture->data[2]; | |
887 | 132 for(i=0;i<height;i++) { /* Cb */ |
18 | 133 put_buffer(pb, ptr1, width); |
134 ptr1 += picture->linesize[1]; | |
135 } | |
887 | 136 for(i=0;i<height;i++) { /* Cr */ |
18 | 137 put_buffer(pb, ptr2, width); |
138 ptr2 += picture->linesize[2]; | |
139 } | |
648
340f1911cd54
add luma only support to yuv4mpeg patch by (Roine Gustafsson <roine users.sourceforge net>)
michael
parents:
634
diff
changeset
|
140 } |
18 | 141 put_flush_packet(pb); |
142 return 0; | |
143 } | |
144 | |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
145 static int yuv4_write_header(AVFormatContext *s) |
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
146 { |
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
147 int* first_pkt = s->priv_data; |
885 | 148 |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
149 if (s->nb_streams != 1) |
482 | 150 return AVERROR_IO; |
885 | 151 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
152 if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) { |
634 | 153 av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n"); |
885 | 154 } |
155 else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) && | |
156 (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) && | |
157 (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
|
158 (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
|
159 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 | 160 return AVERROR_IO; |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
161 } |
885 | 162 |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
163 *first_pkt = 1; |
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
164 return 0; |
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
165 } |
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
166 |
18 | 167 static int yuv4_write_trailer(AVFormatContext *s) |
168 { | |
169 return 0; | |
170 } | |
171 | |
1169 | 172 #ifdef CONFIG_YUV4MPEGPIPE_MUXER |
1167 | 173 AVOutputFormat yuv4mpegpipe_muxer = { |
18 | 174 "yuv4mpegpipe", |
175 "YUV4MPEG pipe format", | |
176 "", | |
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
|
177 "y4m", |
284
c77ce17451a1
* providing MPEG codecs with a generic fields in AVFrame to use.
romansh
parents:
277
diff
changeset
|
178 sizeof(int), |
18 | 179 CODEC_ID_NONE, |
180 CODEC_ID_RAWVIDEO, | |
181 yuv4_write_header, | |
182 yuv4_write_packet, | |
183 yuv4_write_trailer, | |
184 .flags = AVFMT_RAWPICTURE, | |
185 }; | |
1169 | 186 #endif |
18 | 187 |
207
7865656658dc
stdin patch by (Charles Yates <charles dot yates at pandora dot be>)
michaelni
parents:
184
diff
changeset
|
188 /* 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
|
189 #define MAX_YUV4_HEADER 80 |
634 | 190 #define MAX_FRAME_HEADER 80 |
18 | 191 |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
192 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
|
193 { |
634 | 194 char header[MAX_YUV4_HEADER+10]; // Include headroom for the longest option |
195 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
|
196 int i; |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
197 ByteIOContext *pb = &s->pb; |
934
8973dbae81e8
Correctly set the interlaced_frame and top_field_first fields.
diego
parents:
896
diff
changeset
|
198 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
|
199 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
|
200 AVStream *st; |
934
8973dbae81e8
Correctly set the interlaced_frame and top_field_first fields.
diego
parents:
896
diff
changeset
|
201 struct frame_attributes *s1 = s->priv_data; |
885 | 202 |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
203 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
|
204 header[i] = get_byte(pb); |
887 | 205 if (header[i] == '\n') { |
206 header[i+1] = 0x20; // Add a space after last option. Makes parsing "444" vs "444alpha" easier. | |
207 header[i+2] = 0; | |
208 break; | |
209 } | |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
210 } |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
211 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
|
212 if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1; |
634 | 213 |
934
8973dbae81e8
Correctly set the interlaced_frame and top_field_first fields.
diego
parents:
896
diff
changeset
|
214 s1->interlaced_frame = 0; |
8973dbae81e8
Correctly set the interlaced_frame and top_field_first fields.
diego
parents:
896
diff
changeset
|
215 s1->top_field_first = 0; |
634 | 216 header_end = &header[i+1]; // Include space |
217 for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) { | |
218 if (*tokstart==0x20) continue; | |
219 switch (*tokstart++) { | |
220 case 'W': // Width. Required. | |
221 width = strtol(tokstart, &tokend, 10); | |
222 tokstart=tokend; | |
223 break; | |
224 case 'H': // Height. Required. | |
225 height = strtol(tokstart, &tokend, 10); | |
226 tokstart=tokend; | |
227 break; | |
228 case 'C': // Color space | |
229 if (strncmp("420jpeg",tokstart,7)==0) | |
230 pix_fmt = PIX_FMT_YUV420P; | |
231 else if (strncmp("420mpeg2",tokstart,8)==0) | |
232 pix_fmt = PIX_FMT_YUV420P; | |
233 else if (strncmp("420paldv", tokstart, 8)==0) | |
234 pix_fmt = PIX_FMT_YUV420P; | |
235 else if (strncmp("411", tokstart, 3)==0) | |
236 pix_fmt = PIX_FMT_YUV411P; | |
237 else if (strncmp("422", tokstart, 3)==0) | |
238 pix_fmt = PIX_FMT_YUV422P; | |
239 else if (strncmp("444alpha", tokstart, 8)==0) { | |
240 av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n"); | |
241 return -1; | |
242 } else if (strncmp("444", tokstart, 3)==0) | |
243 pix_fmt = PIX_FMT_YUV444P; | |
244 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
|
245 pix_fmt = PIX_FMT_GRAY8; |
634 | 246 } else { |
247 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n"); | |
248 return -1; | |
249 } | |
250 while(tokstart<header_end&&*tokstart!=0x20) tokstart++; | |
251 break; | |
252 case 'I': // Interlace type | |
253 switch (*tokstart++){ | |
254 case '?': | |
255 break; | |
256 case 'p': | |
934
8973dbae81e8
Correctly set the interlaced_frame and top_field_first fields.
diego
parents:
896
diff
changeset
|
257 s1->interlaced_frame=0; |
634 | 258 break; |
259 case 't': | |
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=1; |
634 | 262 break; |
263 case 'b': | |
934
8973dbae81e8
Correctly set the interlaced_frame and top_field_first fields.
diego
parents:
896
diff
changeset
|
264 s1->interlaced_frame=1; |
8973dbae81e8
Correctly set the interlaced_frame and top_field_first fields.
diego
parents:
896
diff
changeset
|
265 s1->top_field_first=0; |
634 | 266 break; |
267 case 'm': | |
268 av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n"); | |
269 return -1; | |
270 default: | |
271 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n"); | |
272 return -1; | |
273 } | |
274 break; | |
275 case 'F': // Frame rate | |
276 sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown | |
277 while(tokstart<header_end&&*tokstart!=0x20) tokstart++; | |
278 break; | |
279 case 'A': // Pixel aspect | |
280 sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown | |
281 while(tokstart<header_end&&*tokstart!=0x20) tokstart++; | |
282 break; | |
283 case 'X': // Vendor extensions | |
284 if (strncmp("YSCSS=",tokstart,6)==0) { | |
285 // Older nonstandard pixel format representation | |
286 tokstart+=6; | |
287 if (strncmp("420JPEG",tokstart,7)==0) | |
288 alt_pix_fmt=PIX_FMT_YUV420P; | |
289 else if (strncmp("420MPEG2",tokstart,8)==0) | |
290 alt_pix_fmt=PIX_FMT_YUV420P; | |
291 else if (strncmp("420PALDV",tokstart,8)==0) | |
292 alt_pix_fmt=PIX_FMT_YUV420P; | |
293 else if (strncmp("411",tokstart,3)==0) | |
294 alt_pix_fmt=PIX_FMT_YUV411P; | |
295 else if (strncmp("422",tokstart,3)==0) | |
296 alt_pix_fmt=PIX_FMT_YUV422P; | |
297 else if (strncmp("444",tokstart,3)==0) | |
298 alt_pix_fmt=PIX_FMT_YUV444P; | |
299 } | |
300 while(tokstart<header_end&&*tokstart!=0x20) tokstart++; | |
301 break; | |
302 } | |
885 | 303 } |
634 | 304 |
305 if ((width == -1) || (height == -1)) { | |
306 av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n"); | |
885 | 307 return -1; |
634 | 308 } |
885 | 309 |
738
dad78387544a
Update yuv4mpeg to use PIX_FMT_NONE patch by (Roine Gustafsson <roine users sourceforge net)
michael
parents:
648
diff
changeset
|
310 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
|
311 if (alt_pix_fmt == PIX_FMT_NONE) |
634 | 312 pix_fmt = PIX_FMT_YUV420P; |
313 else | |
314 pix_fmt = alt_pix_fmt; | |
315 } | |
316 | |
317 if (raten == 0 && rated == 0) { | |
318 // Frame rate unknown | |
319 raten = 25; | |
320 rated = 1; | |
321 } | |
322 | |
323 if (aspectn == 0 && aspectd == 0) { | |
324 // Pixel aspect unknown | |
325 aspectd = 1; | |
326 } | |
885 | 327 |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
328 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
|
329 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
|
330 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
|
331 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
|
332 av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1); |
743 | 333 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
|
334 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
|
335 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
|
336 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
|
337 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
|
338 |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
339 return 0; |
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 |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
342 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
|
343 { |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
344 int i; |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
345 char header[MAX_FRAME_HEADER+1]; |
838 | 346 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
|
347 AVStream *st = s->streams[0]; |
934
8973dbae81e8
Correctly set the interlaced_frame and top_field_first fields.
diego
parents:
896
diff
changeset
|
348 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
|
349 |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
350 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
|
351 header[i] = get_byte(&s->pb); |
887 | 352 if (header[i] == '\n') { |
353 header[i+1] = 0; | |
354 break; | |
355 } | |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
356 } |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
357 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
|
358 if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1; |
885 | 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 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
|
361 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
|
362 |
820
feca73904e67
changing AVCodecContext codec -> *codec in AVStream so additions to AVCodecContext dont randomize AVStream and break binary compatibility
michael
parents:
775
diff
changeset
|
363 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
|
364 if (packet_size < 0) |
537 | 365 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
|
366 |
775 | 367 if (av_get_packet(&s->pb, pkt, packet_size) != packet_size) |
482 | 368 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
|
369 |
934
8973dbae81e8
Correctly set the interlaced_frame and top_field_first fields.
diego
parents:
896
diff
changeset
|
370 if (s->streams[0]->codec->coded_frame) { |
8973dbae81e8
Correctly set the interlaced_frame and top_field_first fields.
diego
parents:
896
diff
changeset
|
371 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
|
372 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
|
373 } |
8973dbae81e8
Correctly set the interlaced_frame and top_field_first fields.
diego
parents:
896
diff
changeset
|
374 |
184
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
375 pkt->stream_index = 0; |
775 | 376 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
|
377 } |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
378 |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
379 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
|
380 { |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
381 return 0; |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
382 } |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
383 |
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
|
384 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
|
385 { |
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 /* 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
|
387 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
|
388 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
|
389 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
|
390 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
|
391 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
|
392 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
|
393 } |
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
|
394 |
1169 | 395 #ifdef CONFIG_YUV4MPEGPIPE_DEMUXER |
1167 | 396 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
|
397 "yuv4mpegpipe", |
2438e76dde67
yuv4mpeg pipe reader for libavformat patch by (D Richard Felker III <dalias at aerifal dot cx>)
michaelni
parents:
178
diff
changeset
|
398 "YUV4MPEG pipe format", |
934
8973dbae81e8
Correctly set the interlaced_frame and top_field_first fields.
diego
parents:
896
diff
changeset
|
399 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
|
400 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
|
401 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
|
402 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
|
403 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
|
404 .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
|
405 }; |
1169 | 406 #endif |