OSDN Git Service

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