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
|
---|
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
18 | */
|
---|
19 | #include "avformat.h"
|
---|
20 |
|
---|
21 | #define Y4M_MAGIC "YUV4MPEG2"
|
---|
22 | #define Y4M_FRAME_MAGIC "FRAME"
|
---|
23 | #define Y4M_LINE_MAX 256
|
---|
24 |
|
---|
25 | struct frame_attributes {
|
---|
26 | int interlaced_frame;
|
---|
27 | int top_field_first;
|
---|
28 | };
|
---|
29 |
|
---|
30 | #ifdef CONFIG_MUXERS
|
---|
31 |
|
---|
32 | static int yuv4_generate_header(AVFormatContext *s, char* buf)
|
---|
33 | {
|
---|
34 | AVStream *st;
|
---|
35 | int width, height;
|
---|
36 | int raten, rated, aspectn, aspectd, n;
|
---|
37 | char inter;
|
---|
38 | const char *colorspace = "";
|
---|
39 |
|
---|
40 | st = s->streams[0];
|
---|
41 | width = st->codec->width;
|
---|
42 | height = st->codec->height;
|
---|
43 |
|
---|
44 | av_reduce(&raten, &rated, st->codec->time_base.den, st->codec->time_base.num, (1UL<<31)-1);
|
---|
45 |
|
---|
46 | aspectn = st->codec->sample_aspect_ratio.num;
|
---|
47 | aspectd = st->codec->sample_aspect_ratio.den;
|
---|
48 |
|
---|
49 | if ( aspectn == 0 && aspectd == 1 ) aspectd = 0; // 0:0 means unknown
|
---|
50 |
|
---|
51 | inter = 'p'; /* progressive is the default */
|
---|
52 | if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame) {
|
---|
53 | inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
|
---|
54 | }
|
---|
55 |
|
---|
56 | switch(st->codec->pix_fmt) {
|
---|
57 | case PIX_FMT_GRAY8:
|
---|
58 | colorspace = " Cmono";
|
---|
59 | break;
|
---|
60 | case PIX_FMT_YUV411P:
|
---|
61 | colorspace = " C411 XYSCSS=411";
|
---|
62 | break;
|
---|
63 | case PIX_FMT_YUV420P:
|
---|
64 | colorspace = (st->codec->codec_id == CODEC_ID_DVVIDEO)?" C420paldv XYSCSS=420PALDV":" C420mpeg2 XYSCSS=420MPEG2";
|
---|
65 | break;
|
---|
66 | case PIX_FMT_YUV422P:
|
---|
67 | colorspace = " C422 XYSCSS=422";
|
---|
68 | break;
|
---|
69 | case PIX_FMT_YUV444P:
|
---|
70 | colorspace = " C444 XYSCSS=444";
|
---|
71 | break;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /* construct stream header, if this is the first frame */
|
---|
75 | n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
|
---|
76 | Y4M_MAGIC,
|
---|
77 | width,
|
---|
78 | height,
|
---|
79 | raten, rated,
|
---|
80 | inter,
|
---|
81 | aspectn, aspectd,
|
---|
82 | colorspace);
|
---|
83 |
|
---|
84 | return n;
|
---|
85 | }
|
---|
86 |
|
---|
87 | static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt)
|
---|
88 | {
|
---|
89 | AVStream *st = s->streams[pkt->stream_index];
|
---|
90 | ByteIOContext *pb = &s->pb;
|
---|
91 | AVPicture *picture;
|
---|
92 | int* first_pkt = s->priv_data;
|
---|
93 | int width, height, h_chroma_shift, v_chroma_shift;
|
---|
94 | int i, m;
|
---|
95 | char buf2[Y4M_LINE_MAX+1];
|
---|
96 | char buf1[20];
|
---|
97 | uint8_t *ptr, *ptr1, *ptr2;
|
---|
98 |
|
---|
99 | picture = (AVPicture *)pkt->data;
|
---|
100 |
|
---|
101 | /* for the first packet we have to output the header as well */
|
---|
102 | if (*first_pkt) {
|
---|
103 | *first_pkt = 0;
|
---|
104 | if (yuv4_generate_header(s, buf2) < 0) {
|
---|
105 | av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n");
|
---|
106 | return AVERROR_IO;
|
---|
107 | } else {
|
---|
108 | put_buffer(pb, buf2, strlen(buf2));
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | /* construct frame header */
|
---|
113 |
|
---|
114 | m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
|
---|
115 | put_buffer(pb, buf1, strlen(buf1));
|
---|
116 |
|
---|
117 | width = st->codec->width;
|
---|
118 | height = st->codec->height;
|
---|
119 |
|
---|
120 | ptr = picture->data[0];
|
---|
121 | for(i=0;i<height;i++) {
|
---|
122 | put_buffer(pb, ptr, width);
|
---|
123 | ptr += picture->linesize[0];
|
---|
124 | }
|
---|
125 |
|
---|
126 | if (st->codec->pix_fmt != PIX_FMT_GRAY8){
|
---|
127 | // Adjust for smaller Cb and Cr planes
|
---|
128 | avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift, &v_chroma_shift);
|
---|
129 | width >>= h_chroma_shift;
|
---|
130 | height >>= v_chroma_shift;
|
---|
131 |
|
---|
132 | ptr1 = picture->data[1];
|
---|
133 | ptr2 = picture->data[2];
|
---|
134 | for(i=0;i<height;i++) { /* Cb */
|
---|
135 | put_buffer(pb, ptr1, width);
|
---|
136 | ptr1 += picture->linesize[1];
|
---|
137 | }
|
---|
138 | for(i=0;i<height;i++) { /* Cr */
|
---|
139 | put_buffer(pb, ptr2, width);
|
---|
140 | ptr2 += picture->linesize[2];
|
---|
141 | }
|
---|
142 | }
|
---|
143 | put_flush_packet(pb);
|
---|
144 | return 0;
|
---|
145 | }
|
---|
146 |
|
---|
147 | static int yuv4_write_header(AVFormatContext *s)
|
---|
148 | {
|
---|
149 | int* first_pkt = s->priv_data;
|
---|
150 |
|
---|
151 | if (s->nb_streams != 1)
|
---|
152 | return AVERROR_IO;
|
---|
153 |
|
---|
154 | if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) {
|
---|
155 | av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
|
---|
156 | }
|
---|
157 | else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
|
---|
158 | (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
|
---|
159 | (s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) &&
|
---|
160 | (s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
|
---|
161 | 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");
|
---|
162 | return AVERROR_IO;
|
---|
163 | }
|
---|
164 |
|
---|
165 | *first_pkt = 1;
|
---|
166 | return 0;
|
---|
167 | }
|
---|
168 |
|
---|
169 | static int yuv4_write_trailer(AVFormatContext *s)
|
---|
170 | {
|
---|
171 | return 0;
|
---|
172 | }
|
---|
173 |
|
---|
174 | AVOutputFormat yuv4mpegpipe_muxer = {
|
---|
175 | "yuv4mpegpipe",
|
---|
176 | "YUV4MPEG pipe format",
|
---|
177 | "",
|
---|
178 | "y4m",
|
---|
179 | sizeof(int),
|
---|
180 | CODEC_ID_NONE,
|
---|
181 | CODEC_ID_RAWVIDEO,
|
---|
182 | yuv4_write_header,
|
---|
183 | yuv4_write_packet,
|
---|
184 | yuv4_write_trailer,
|
---|
185 | .flags = AVFMT_RAWPICTURE,
|
---|
186 | };
|
---|
187 | #endif //CONFIG_MUXERS
|
---|
188 |
|
---|
189 | /* Header size increased to allow room for optional flags */
|
---|
190 | #define MAX_YUV4_HEADER 80
|
---|
191 | #define MAX_FRAME_HEADER 80
|
---|
192 |
|
---|
193 | static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
|
---|
194 | {
|
---|
195 | char header[MAX_YUV4_HEADER+10]; // Include headroom for the longest option
|
---|
196 | char *tokstart,*tokend,*header_end;
|
---|
197 | int i;
|
---|
198 | ByteIOContext *pb = &s->pb;
|
---|
199 | int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0;
|
---|
200 | enum PixelFormat pix_fmt=PIX_FMT_NONE,alt_pix_fmt=PIX_FMT_NONE;
|
---|
201 | AVStream *st;
|
---|
202 | struct frame_attributes *s1 = s->priv_data;
|
---|
203 |
|
---|
204 | for (i=0; i<MAX_YUV4_HEADER; i++) {
|
---|
205 | header[i] = get_byte(pb);
|
---|
206 | if (header[i] == '\n') {
|
---|
207 | header[i+1] = 0x20; // Add a space after last option. Makes parsing "444" vs "444alpha" easier.
|
---|
208 | header[i+2] = 0;
|
---|
209 | break;
|
---|
210 | }
|
---|
211 | }
|
---|
212 | if (i == MAX_YUV4_HEADER) return -1;
|
---|
213 | if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
|
---|
214 |
|
---|
215 | s1->interlaced_frame = 0;
|
---|
216 | s1->top_field_first = 0;
|
---|
217 | header_end = &header[i+1]; // Include space
|
---|
218 | for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) {
|
---|
219 | if (*tokstart==0x20) continue;
|
---|
220 | switch (*tokstart++) {
|
---|
221 | case 'W': // Width. Required.
|
---|
222 | width = strtol(tokstart, &tokend, 10);
|
---|
223 | tokstart=tokend;
|
---|
224 | break;
|
---|
225 | case 'H': // Height. Required.
|
---|
226 | height = strtol(tokstart, &tokend, 10);
|
---|
227 | tokstart=tokend;
|
---|
228 | break;
|
---|
229 | case 'C': // Color space
|
---|
230 | if (strncmp("420jpeg",tokstart,7)==0)
|
---|
231 | pix_fmt = PIX_FMT_YUV420P;
|
---|
232 | else if (strncmp("420mpeg2",tokstart,8)==0)
|
---|
233 | pix_fmt = PIX_FMT_YUV420P;
|
---|
234 | else if (strncmp("420paldv", tokstart, 8)==0)
|
---|
235 | pix_fmt = PIX_FMT_YUV420P;
|
---|
236 | else if (strncmp("411", tokstart, 3)==0)
|
---|
237 | pix_fmt = PIX_FMT_YUV411P;
|
---|
238 | else if (strncmp("422", tokstart, 3)==0)
|
---|
239 | pix_fmt = PIX_FMT_YUV422P;
|
---|
240 | else if (strncmp("444alpha", tokstart, 8)==0) {
|
---|
241 | av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n");
|
---|
242 | return -1;
|
---|
243 | } else if (strncmp("444", tokstart, 3)==0)
|
---|
244 | pix_fmt = PIX_FMT_YUV444P;
|
---|
245 | else if (strncmp("mono",tokstart, 4)==0) {
|
---|
246 | pix_fmt = PIX_FMT_GRAY8;
|
---|
247 | } else {
|
---|
248 | av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n");
|
---|
249 | return -1;
|
---|
250 | }
|
---|
251 | while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
|
---|
252 | break;
|
---|
253 | case 'I': // Interlace type
|
---|
254 | switch (*tokstart++){
|
---|
255 | case '?':
|
---|
256 | break;
|
---|
257 | case 'p':
|
---|
258 | s1->interlaced_frame=0;
|
---|
259 | break;
|
---|
260 | case 't':
|
---|
261 | s1->interlaced_frame=1;
|
---|
262 | s1->top_field_first=1;
|
---|
263 | break;
|
---|
264 | case 'b':
|
---|
265 | s1->interlaced_frame=1;
|
---|
266 | s1->top_field_first=0;
|
---|
267 | break;
|
---|
268 | case 'm':
|
---|
269 | av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
|
---|
270 | return -1;
|
---|
271 | default:
|
---|
272 | av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
|
---|
273 | return -1;
|
---|
274 | }
|
---|
275 | break;
|
---|
276 | case 'F': // Frame rate
|
---|
277 | sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown
|
---|
278 | while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
|
---|
279 | break;
|
---|
280 | case 'A': // Pixel aspect
|
---|
281 | sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown
|
---|
282 | while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
|
---|
283 | break;
|
---|
284 | case 'X': // Vendor extensions
|
---|
285 | if (strncmp("YSCSS=",tokstart,6)==0) {
|
---|
286 | // Older nonstandard pixel format representation
|
---|
287 | tokstart+=6;
|
---|
288 | if (strncmp("420JPEG",tokstart,7)==0)
|
---|
289 | alt_pix_fmt=PIX_FMT_YUV420P;
|
---|
290 | else if (strncmp("420MPEG2",tokstart,8)==0)
|
---|
291 | alt_pix_fmt=PIX_FMT_YUV420P;
|
---|
292 | else if (strncmp("420PALDV",tokstart,8)==0)
|
---|
293 | alt_pix_fmt=PIX_FMT_YUV420P;
|
---|
294 | else if (strncmp("411",tokstart,3)==0)
|
---|
295 | alt_pix_fmt=PIX_FMT_YUV411P;
|
---|
296 | else if (strncmp("422",tokstart,3)==0)
|
---|
297 | alt_pix_fmt=PIX_FMT_YUV422P;
|
---|
298 | else if (strncmp("444",tokstart,3)==0)
|
---|
299 | alt_pix_fmt=PIX_FMT_YUV444P;
|
---|
300 | }
|
---|
301 | while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
|
---|
302 | break;
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | if ((width == -1) || (height == -1)) {
|
---|
307 | av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
|
---|
308 | return -1;
|
---|
309 | }
|
---|
310 |
|
---|
311 | if (pix_fmt == PIX_FMT_NONE) {
|
---|
312 | if (alt_pix_fmt == PIX_FMT_NONE)
|
---|
313 | pix_fmt = PIX_FMT_YUV420P;
|
---|
314 | else
|
---|
315 | pix_fmt = alt_pix_fmt;
|
---|
316 | }
|
---|
317 |
|
---|
318 | if (raten == 0 && rated == 0) {
|
---|
319 | // Frame rate unknown
|
---|
320 | raten = 25;
|
---|
321 | rated = 1;
|
---|
322 | }
|
---|
323 |
|
---|
324 | if (aspectn == 0 && aspectd == 0) {
|
---|
325 | // Pixel aspect unknown
|
---|
326 | aspectd = 1;
|
---|
327 | }
|
---|
328 |
|
---|
329 | st = av_new_stream(s, 0);
|
---|
330 | st = s->streams[0];
|
---|
331 | st->codec->width = width;
|
---|
332 | st->codec->height = height;
|
---|
333 | av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
|
---|
334 | av_set_pts_info(st, 64, rated, raten);
|
---|
335 | st->codec->pix_fmt = pix_fmt;
|
---|
336 | st->codec->codec_type = CODEC_TYPE_VIDEO;
|
---|
337 | st->codec->codec_id = CODEC_ID_RAWVIDEO;
|
---|
338 | st->codec->sample_aspect_ratio= (AVRational){aspectn, aspectd};
|
---|
339 |
|
---|
340 | return 0;
|
---|
341 | }
|
---|
342 |
|
---|
343 | static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
|
---|
344 | {
|
---|
345 | int i;
|
---|
346 | char header[MAX_FRAME_HEADER+1];
|
---|
347 | int packet_size, width, height;
|
---|
348 | AVStream *st = s->streams[0];
|
---|
349 | struct frame_attributes *s1 = s->priv_data;
|
---|
350 |
|
---|
351 | for (i=0; i<MAX_FRAME_HEADER; i++) {
|
---|
352 | header[i] = get_byte(&s->pb);
|
---|
353 | if (header[i] == '\n') {
|
---|
354 | header[i+1] = 0;
|
---|
355 | break;
|
---|
356 | }
|
---|
357 | }
|
---|
358 | if (i == MAX_FRAME_HEADER) return -1;
|
---|
359 | if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1;
|
---|
360 |
|
---|
361 | width = st->codec->width;
|
---|
362 | height = st->codec->height;
|
---|
363 |
|
---|
364 | packet_size = avpicture_get_size(st->codec->pix_fmt, width, height);
|
---|
365 | if (packet_size < 0)
|
---|
366 | return -1;
|
---|
367 |
|
---|
368 | if (av_get_packet(&s->pb, pkt, packet_size) != packet_size)
|
---|
369 | return AVERROR_IO;
|
---|
370 |
|
---|
371 | if (s->streams[0]->codec->coded_frame) {
|
---|
372 | s->streams[0]->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
|
---|
373 | s->streams[0]->codec->coded_frame->top_field_first = s1->top_field_first;
|
---|
374 | }
|
---|
375 |
|
---|
376 | pkt->stream_index = 0;
|
---|
377 | return 0;
|
---|
378 | }
|
---|
379 |
|
---|
380 | static int yuv4_read_close(AVFormatContext *s)
|
---|
381 | {
|
---|
382 | return 0;
|
---|
383 | }
|
---|
384 |
|
---|
385 | static int yuv4_probe(AVProbeData *pd)
|
---|
386 | {
|
---|
387 | /* check file header */
|
---|
388 | if (pd->buf_size <= sizeof(Y4M_MAGIC))
|
---|
389 | return 0;
|
---|
390 | if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1)==0)
|
---|
391 | return AVPROBE_SCORE_MAX;
|
---|
392 | else
|
---|
393 | return 0;
|
---|
394 | }
|
---|
395 |
|
---|
396 | AVInputFormat yuv4mpegpipe_demuxer = {
|
---|
397 | "yuv4mpegpipe",
|
---|
398 | "YUV4MPEG pipe format",
|
---|
399 | sizeof(struct frame_attributes),
|
---|
400 | yuv4_probe,
|
---|
401 | yuv4_read_header,
|
---|
402 | yuv4_read_packet,
|
---|
403 | yuv4_read_close,
|
---|
404 | .extensions = "y4m"
|
---|
405 | };
|
---|
406 |
|
---|
407 | int yuv4mpeg_init(void)
|
---|
408 | {
|
---|
409 | av_register_input_format(&yuv4mpegpipe_demuxer);
|
---|
410 | #ifdef CONFIG_MUXERS
|
---|
411 | av_register_output_format(&yuv4mpegpipe_muxer);
|
---|
412 | #endif //CONFIG_MUXERS
|
---|
413 | return 0;
|
---|
414 | }
|
---|
415 |
|
---|