Revision 2d745414 libavformat/yuv4mpeg.c

View differences:

libavformat/yuv4mpeg.c
30 30
    int width, height;
31 31
    int raten, rated, aspectn, aspectd, n;
32 32
    char inter;
33
    char *colorspace = "";
33 34

  
34 35
    st = s->streams[0];
35 36
    width = st->codec.width;
......
40 41
    aspectn = st->codec.sample_aspect_ratio.num;
41 42
    aspectd = st->codec.sample_aspect_ratio.den;
42 43
    
44
    if ( aspectn == 0 && aspectd == 1 ) aspectd = 0;  // 0:0 means unknown
45

  
43 46
    inter = 'p'; /* progressive is the default */
44 47
    if (st->codec.coded_frame && st->codec.coded_frame->interlaced_frame) {
45 48
        inter = st->codec.coded_frame->top_field_first ? 't' : 'b';
46 49
    }
47 50

  
51
    switch(st->codec.pix_fmt) {
52
    case PIX_FMT_YUV411P:
53
        colorspace = " C411 XYSCSS=411";
54
        break;
55
    case PIX_FMT_YUV420P:
56
        colorspace = (st->codec.codec_id == CODEC_ID_DVVIDEO)?" C420paldv XYSCSS=420PALDV":" C420mpeg2 XYSCSS=420MPEG2";
57
        break;
58
    case PIX_FMT_YUV422P:
59
        colorspace = " C422 XYSCSS=422";
60
        break;
61
    case PIX_FMT_YUV444P:
62
        colorspace = " C444 XYSCSS=444";
63
        break;
64
    }
65

  
48 66
    /* construct stream header, if this is the first frame */
49 67
    n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n",
50 68
                 Y4M_MAGIC,
......
53 71
                 raten, rated,
54 72
                 inter,
55 73
                 aspectn, aspectd,
56
		 (st->codec.pix_fmt == PIX_FMT_YUV411P) ? " C411 XYSCSS=411" : " C420mpeg2 XYSCSS=420MPEG2");
74
                 colorspace);
57 75
		 
58 76
    return n;
59 77
}
......
64 82
    ByteIOContext *pb = &s->pb;
65 83
    AVPicture *picture;
66 84
    int* first_pkt = s->priv_data;
67
    int width, height;
85
    int width, height, h_chroma_shift, v_chroma_shift;
68 86
    int i, m;
69 87
    char buf2[Y4M_LINE_MAX+1];
70 88
    char buf1[20];
......
97 115
        ptr += picture->linesize[0];
98 116
    }
99 117

  
100
    height >>= 1;
101
    width >>= 1;
118
    // Adjust for smaller Cb and Cr planes
119
    avcodec_get_chroma_sub_sample(st->codec.pix_fmt, &h_chroma_shift, &v_chroma_shift);
120
    width >>= h_chroma_shift;
121
    height >>= v_chroma_shift;
122

  
102 123
    ptr1 = picture->data[1];
103 124
    ptr2 = picture->data[2];
104 125
    for(i=0;i<height;i++) {		/* Cb */
......
121 142
        return AVERROR_IO;
122 143
    
123 144
    if (s->streams[0]->codec.pix_fmt == PIX_FMT_YUV411P) {
124
        av_log(s, AV_LOG_ERROR, "Warning: generating non-standard 4:1:1 YUV stream, some mjpegtools might not work.\n");
145
        av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
125 146
    } 
126
    else if (s->streams[0]->codec.pix_fmt != PIX_FMT_YUV420P) {
127
        av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles 4:2:0, 4:1:1 YUV data. Use -pix_fmt to select one.\n");
147
    else if ((s->streams[0]->codec.pix_fmt != PIX_FMT_YUV420P) && 
148
             (s->streams[0]->codec.pix_fmt != PIX_FMT_YUV422P) && 
149
             (s->streams[0]->codec.pix_fmt != PIX_FMT_YUV444P)) {
150
        av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles 4:4:4, 4:2:2, 4:2:0 and 4:1:1 planar YUV data. Use -pix_fmt to select one.\n");
128 151
	return AVERROR_IO;
129 152
    }
130 153
    
......
154 177

  
155 178
/* Header size increased to allow room for optional flags */
156 179
#define MAX_YUV4_HEADER 80
157
#define MAX_FRAME_HEADER 10
180
#define MAX_FRAME_HEADER 80
158 181

  
159 182
static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
160 183
{
161
    char header[MAX_YUV4_HEADER+1];
184
    char header[MAX_YUV4_HEADER+10];  // Include headroom for the longest option
185
    char *tokstart,*tokend,*header_end;
162 186
    int i;
163 187
    ByteIOContext *pb = &s->pb;
164
    int width, height, raten, rated, aspectn, aspectd;
165
    char lacing;
188
    int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0,interlaced_frame=0,top_field_first=0;
189
    enum PixelFormat pix_fmt=PIX_FMT_NB,alt_pix_fmt=PIX_FMT_NB;
166 190
    AVStream *st;
167 191
    
168 192
    for (i=0; i<MAX_YUV4_HEADER; i++) {
169 193
        header[i] = get_byte(pb);
170 194
	if (header[i] == '\n') {
171
	    header[i+1] = 0;
195
	    header[i+1] = 0x20;  // Add a space after last option. Makes parsing "444" vs "444alpha" easier.
196
	    header[i+2] = 0;
172 197
	    break;
173 198
	}
174 199
    }
175 200
    if (i == MAX_YUV4_HEADER) return -1;
176 201
    if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
177
    sscanf(header+strlen(Y4M_MAGIC), " W%d H%d F%d:%d I%c A%d:%d",
178
           &width, &height, &raten, &rated, &lacing, &aspectn, &aspectd);
202

  
203
    header_end = &header[i+1]; // Include space
204
    for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) {
205
        if (*tokstart==0x20) continue;
206
        switch (*tokstart++) {
207
        case 'W': // Width. Required.
208
            width = strtol(tokstart, &tokend, 10);
209
            tokstart=tokend;
210
            break;
211
        case 'H': // Height. Required.
212
            height = strtol(tokstart, &tokend, 10);
213
            tokstart=tokend;
214
            break;
215
        case 'C': // Color space
216
            if (strncmp("420jpeg",tokstart,7)==0)
217
                pix_fmt = PIX_FMT_YUV420P;
218
            else if (strncmp("420mpeg2",tokstart,8)==0)
219
                pix_fmt = PIX_FMT_YUV420P;
220
            else if (strncmp("420paldv", tokstart, 8)==0)
221
                pix_fmt = PIX_FMT_YUV420P;
222
            else if (strncmp("411", tokstart, 3)==0)
223
                pix_fmt = PIX_FMT_YUV411P;
224
            else if (strncmp("422", tokstart, 3)==0)
225
                pix_fmt = PIX_FMT_YUV422P;
226
            else if (strncmp("444alpha", tokstart, 8)==0) {
227
                av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n");
228
                return -1;
229
            } else if (strncmp("444", tokstart, 3)==0)
230
                pix_fmt = PIX_FMT_YUV444P;
231
            else if (strncmp("mono",tokstart, 4)==0) {
232
                av_log(s, AV_LOG_ERROR, "Cannot handle luma only YUV4MPEG stream.\n");
233
                return -1;
234
            } else {
235
                av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n");
236
                return -1;
237
            }
238
            while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
239
            break;
240
        case 'I': // Interlace type
241
            switch (*tokstart++){
242
            case '?':
243
                break;
244
            case 'p':
245
                interlaced_frame=0;
246
                break;
247
            case 't':
248
                interlaced_frame=1;
249
                top_field_first=1;
250
                break;
251
            case 'b':
252
                interlaced_frame=1;
253
                top_field_first=0;
254
                break;
255
            case 'm':
256
                av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
257
                return -1;
258
            default:
259
                av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
260
                return -1;
261
            }
262
            break;
263
        case 'F': // Frame rate
264
            sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown
265
            while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
266
            break;
267
        case 'A': // Pixel aspect
268
            sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown
269
            while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
270
            break;
271
        case 'X': // Vendor extensions
272
            if (strncmp("YSCSS=",tokstart,6)==0) {
273
                // Older nonstandard pixel format representation
274
                tokstart+=6;
275
                if (strncmp("420JPEG",tokstart,7)==0)
276
                    alt_pix_fmt=PIX_FMT_YUV420P;
277
                else if (strncmp("420MPEG2",tokstart,8)==0)
278
                    alt_pix_fmt=PIX_FMT_YUV420P;
279
                else if (strncmp("420PALDV",tokstart,8)==0)
280
                    alt_pix_fmt=PIX_FMT_YUV420P;
281
                else if (strncmp("411",tokstart,3)==0)
282
                    alt_pix_fmt=PIX_FMT_YUV411P;
283
                else if (strncmp("422",tokstart,3)==0)
284
                    alt_pix_fmt=PIX_FMT_YUV422P;
285
                else if (strncmp("444",tokstart,3)==0)
286
                    alt_pix_fmt=PIX_FMT_YUV444P;
287
            }
288
            while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
289
            break;
290
        }
291
    }            
292

  
293
    if ((width == -1) || (height == -1)) {
294
        av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
295
        return -1;        
296
    }
179 297
    
298
    if (pix_fmt == PIX_FMT_NB) {
299
        if (alt_pix_fmt == PIX_FMT_NB)
300
            pix_fmt = PIX_FMT_YUV420P;
301
        else
302
            pix_fmt = alt_pix_fmt;
303
    }
304

  
305
    if (raten == 0 && rated == 0) {
306
        // Frame rate unknown
307
        raten = 25;
308
        rated = 1;
309
    }
310

  
311
    if (aspectn == 0 && aspectd == 0) {
312
        // Pixel aspect unknown
313
        aspectd = 1;
314
    }
315
        
180 316
    st = av_new_stream(s, 0);
181 317
    st = s->streams[0];
182 318
    st->codec.width = width;
......
184 320
    av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
185 321
    st->codec.frame_rate = raten;
186 322
    st->codec.frame_rate_base = rated;
187
    st->codec.pix_fmt = PIX_FMT_YUV420P;
323
    st->codec.pix_fmt = pix_fmt;
188 324
    st->codec.codec_type = CODEC_TYPE_VIDEO;
189 325
    st->codec.codec_id = CODEC_ID_RAWVIDEO;
190 326
    st->codec.sample_aspect_ratio= (AVRational){aspectn, aspectd};

Also available in: Unified diff