3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #define Y4M_MAGIC "YUV4MPEG2"
24 #define Y4M_FRAME_MAGIC "FRAME"
25 #define Y4M_LINE_MAX 256
27 struct frame_attributes
{
32 #ifdef CONFIG_YUV4MPEGPIPE_MUXER
33 static int yuv4_generate_header(AVFormatContext
*s
, char* buf
)
37 int raten
, rated
, aspectn
, aspectd
, n
;
39 const char *colorspace
= "";
42 width
= st
->codec
->width
;
43 height
= st
->codec
->height
;
45 av_reduce(&raten
, &rated
, st
->codec
->time_base
.den
, st
->codec
->time_base
.num
, (1UL<<31)-1);
47 aspectn
= st
->sample_aspect_ratio
.num
;
48 aspectd
= st
->sample_aspect_ratio
.den
;
50 if ( aspectn
== 0 && aspectd
== 1 ) aspectd
= 0; // 0:0 means unknown
52 inter
= 'p'; /* progressive is the default */
53 if (st
->codec
->coded_frame
&& st
->codec
->coded_frame
->interlaced_frame
) {
54 inter
= st
->codec
->coded_frame
->top_field_first
? 't' : 'b';
57 switch(st
->codec
->pix_fmt
) {
59 colorspace
= " Cmono";
62 colorspace
= " C411 XYSCSS=411";
65 colorspace
= (st
->codec
->codec_id
== CODEC_ID_DVVIDEO
)?" C420paldv XYSCSS=420PALDV":" C420mpeg2 XYSCSS=420MPEG2";
68 colorspace
= " C422 XYSCSS=422";
71 colorspace
= " C444 XYSCSS=444";
75 /* construct stream header, if this is the first frame */
76 n
= snprintf(buf
, Y4M_LINE_MAX
, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
88 static int yuv4_write_packet(AVFormatContext
*s
, AVPacket
*pkt
)
90 AVStream
*st
= s
->streams
[pkt
->stream_index
];
91 ByteIOContext
*pb
= s
->pb
;
93 int* first_pkt
= s
->priv_data
;
94 int width
, height
, h_chroma_shift
, v_chroma_shift
;
96 char buf2
[Y4M_LINE_MAX
+1];
98 uint8_t *ptr
, *ptr1
, *ptr2
;
100 picture
= (AVPicture
*)pkt
->data
;
102 /* for the first packet we have to output the header as well */
105 if (yuv4_generate_header(s
, buf2
) < 0) {
106 av_log(s
, AV_LOG_ERROR
, "Error. YUV4MPEG stream header write failed.\n");
109 put_buffer(pb
, buf2
, strlen(buf2
));
113 /* construct frame header */
115 m
= snprintf(buf1
, sizeof(buf1
), "%s\n", Y4M_FRAME_MAGIC
);
116 put_buffer(pb
, buf1
, strlen(buf1
));
118 width
= st
->codec
->width
;
119 height
= st
->codec
->height
;
121 ptr
= picture
->data
[0];
122 for(i
=0;i
<height
;i
++) {
123 put_buffer(pb
, ptr
, width
);
124 ptr
+= picture
->linesize
[0];
127 if (st
->codec
->pix_fmt
!= PIX_FMT_GRAY8
){
128 // Adjust for smaller Cb and Cr planes
129 avcodec_get_chroma_sub_sample(st
->codec
->pix_fmt
, &h_chroma_shift
, &v_chroma_shift
);
130 width
>>= h_chroma_shift
;
131 height
>>= v_chroma_shift
;
133 ptr1
= picture
->data
[1];
134 ptr2
= picture
->data
[2];
135 for(i
=0;i
<height
;i
++) { /* Cb */
136 put_buffer(pb
, ptr1
, width
);
137 ptr1
+= picture
->linesize
[1];
139 for(i
=0;i
<height
;i
++) { /* Cr */
140 put_buffer(pb
, ptr2
, width
);
141 ptr2
+= picture
->linesize
[2];
144 put_flush_packet(pb
);
148 static int yuv4_write_header(AVFormatContext
*s
)
150 int* first_pkt
= s
->priv_data
;
152 if (s
->nb_streams
!= 1)
155 if (s
->streams
[0]->codec
->pix_fmt
== PIX_FMT_YUV411P
) {
156 av_log(s
, AV_LOG_ERROR
, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
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
) &&
161 (s
->streams
[0]->codec
->pix_fmt
!= PIX_FMT_YUV444P
)) {
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");
170 AVOutputFormat yuv4mpegpipe_muxer
= {
172 NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
180 .flags
= AVFMT_RAWPICTURE
,
184 /* Header size increased to allow room for optional flags */
185 #define MAX_YUV4_HEADER 80
186 #define MAX_FRAME_HEADER 80
188 static int yuv4_read_header(AVFormatContext
*s
, AVFormatParameters
*ap
)
190 char header
[MAX_YUV4_HEADER
+10]; // Include headroom for the longest option
191 char *tokstart
,*tokend
,*header_end
;
193 ByteIOContext
*pb
= s
->pb
;
194 int width
=-1, height
=-1, raten
=0, rated
=0, aspectn
=0, aspectd
=0;
195 enum PixelFormat pix_fmt
=PIX_FMT_NONE
,alt_pix_fmt
=PIX_FMT_NONE
;
197 struct frame_attributes
*s1
= s
->priv_data
;
199 for (i
=0; i
<MAX_YUV4_HEADER
; i
++) {
200 header
[i
] = get_byte(pb
);
201 if (header
[i
] == '\n') {
202 header
[i
+1] = 0x20; // Add a space after last option. Makes parsing "444" vs "444alpha" easier.
207 if (i
== MAX_YUV4_HEADER
) return -1;
208 if (strncmp(header
, Y4M_MAGIC
, strlen(Y4M_MAGIC
))) return -1;
210 s1
->interlaced_frame
= 0;
211 s1
->top_field_first
= 0;
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);
220 case 'H': // Height. Required.
221 height
= strtol(tokstart
, &tokend
, 10);
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");
238 } else if (strncmp("444", tokstart
, 3)==0)
239 pix_fmt
= PIX_FMT_YUV444P
;
240 else if (strncmp("mono",tokstart
, 4)==0) {
241 pix_fmt
= PIX_FMT_GRAY8
;
243 av_log(s
, AV_LOG_ERROR
, "YUV4MPEG stream contains an unknown pixel format.\n");
246 while(tokstart
<header_end
&&*tokstart
!=0x20) tokstart
++;
248 case 'I': // Interlace type
249 switch (*tokstart
++){
253 s1
->interlaced_frame
=0;
256 s1
->interlaced_frame
=1;
257 s1
->top_field_first
=1;
260 s1
->interlaced_frame
=1;
261 s1
->top_field_first
=0;
264 av_log(s
, AV_LOG_ERROR
, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
267 av_log(s
, AV_LOG_ERROR
, "YUV4MPEG has invalid header.\n");
271 case 'F': // Frame rate
272 sscanf(tokstart
,"%d:%d",&raten
,&rated
); // 0:0 if unknown
273 while(tokstart
<header_end
&&*tokstart
!=0x20) tokstart
++;
275 case 'A': // Pixel aspect
276 sscanf(tokstart
,"%d:%d",&aspectn
,&aspectd
); // 0:0 if unknown
277 while(tokstart
<header_end
&&*tokstart
!=0x20) tokstart
++;
279 case 'X': // Vendor extensions
280 if (strncmp("YSCSS=",tokstart
,6)==0) {
281 // Older nonstandard pixel format representation
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
;
296 while(tokstart
<header_end
&&*tokstart
!=0x20) tokstart
++;
301 if ((width
== -1) || (height
== -1)) {
302 av_log(s
, AV_LOG_ERROR
, "YUV4MPEG has invalid header.\n");
306 if (pix_fmt
== PIX_FMT_NONE
) {
307 if (alt_pix_fmt
== PIX_FMT_NONE
)
308 pix_fmt
= PIX_FMT_YUV420P
;
310 pix_fmt
= alt_pix_fmt
;
313 if (raten
== 0 && rated
== 0) {
314 // Frame rate unknown
319 if (aspectn
== 0 && aspectd
== 0) {
320 // Pixel aspect unknown
324 st
= av_new_stream(s
, 0);
326 return AVERROR(ENOMEM
);
327 st
->codec
->width
= width
;
328 st
->codec
->height
= height
;
329 av_reduce(&raten
, &rated
, raten
, rated
, (1UL<<31)-1);
330 av_set_pts_info(st
, 64, rated
, raten
);
331 st
->codec
->pix_fmt
= pix_fmt
;
332 st
->codec
->codec_type
= CODEC_TYPE_VIDEO
;
333 st
->codec
->codec_id
= CODEC_ID_RAWVIDEO
;
334 st
->sample_aspect_ratio
= (AVRational
){aspectn
, aspectd
};
339 static int yuv4_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
342 char header
[MAX_FRAME_HEADER
+1];
343 int packet_size
, width
, height
;
344 AVStream
*st
= s
->streams
[0];
345 struct frame_attributes
*s1
= s
->priv_data
;
347 for (i
=0; i
<MAX_FRAME_HEADER
; i
++) {
348 header
[i
] = get_byte(s
->pb
);
349 if (header
[i
] == '\n') {
354 if (i
== MAX_FRAME_HEADER
) return -1;
355 if (strncmp(header
, Y4M_FRAME_MAGIC
, strlen(Y4M_FRAME_MAGIC
))) return -1;
357 width
= st
->codec
->width
;
358 height
= st
->codec
->height
;
360 packet_size
= avpicture_get_size(st
->codec
->pix_fmt
, width
, height
);
364 if (av_get_packet(s
->pb
, pkt
, packet_size
) != packet_size
)
367 if (s
->streams
[0]->codec
->coded_frame
) {
368 s
->streams
[0]->codec
->coded_frame
->interlaced_frame
= s1
->interlaced_frame
;
369 s
->streams
[0]->codec
->coded_frame
->top_field_first
= s1
->top_field_first
;
372 pkt
->stream_index
= 0;
376 static int yuv4_probe(AVProbeData
*pd
)
378 /* check file header */
379 if (strncmp(pd
->buf
, Y4M_MAGIC
, sizeof(Y4M_MAGIC
)-1)==0)
380 return AVPROBE_SCORE_MAX
;
385 #ifdef CONFIG_YUV4MPEGPIPE_DEMUXER
386 AVInputFormat yuv4mpegpipe_demuxer
= {
388 NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"),
389 sizeof(struct frame_attributes
),