Statistics
| Branch: | Revision:

ffmpeg / libavformat / yuv4mpeg.c @ 1ae7518d

History | View | Annotate | Download (12.6 KB)

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