39
40
const char *colorspace = "";
42
width = st->codec->width;
43
width = st->codec->width;
43
44
height = st->codec->height;
45
av_reduce(&raten, &rated, st->codec->time_base.den, st->codec->time_base.num, (1UL<<31)-1);
46
av_reduce(&raten, &rated, st->codec->time_base.den,
47
st->codec->time_base.num, (1UL << 31) - 1);
47
49
aspectn = st->sample_aspect_ratio.num;
48
50
aspectd = st->sample_aspect_ratio.den;
50
if ( aspectn == 0 && aspectd == 1 ) aspectd = 0; // 0:0 means unknown
52
if (aspectn == 0 && aspectd == 1)
53
aspectd = 0; // 0:0 means unknown
52
55
inter = 'p'; /* progressive is the default */
53
if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame) {
56
if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame)
54
57
inter = st->codec->coded_frame->top_field_first ? 't' : 'b';
57
switch(st->codec->pix_fmt) {
59
switch (st->codec->pix_fmt) {
58
60
case PIX_FMT_GRAY8:
59
61
colorspace = " Cmono";
117
117
snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC);
118
118
avio_write(pb, buf1, strlen(buf1));
120
width = st->codec->width;
120
width = st->codec->width;
121
121
height = st->codec->height;
123
123
ptr = picture->data[0];
124
for(i=0;i<height;i++) {
124
for (i = 0; i < height; i++) {
125
125
avio_write(pb, ptr, width);
126
126
ptr += picture->linesize[0];
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;
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,
133
width >>= h_chroma_shift;
134
height >>= v_chroma_shift;
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];
141
for(i=0;i<height;i++) { /* Cr */
142
avio_write(pb, ptr2, width);
136
ptr1 = picture->data[1];
137
ptr2 = picture->data[2];
138
for (i = 0; i < height; i++) { /* Cb */
139
avio_write(pb, ptr1, width);
140
ptr1 += picture->linesize[1];
142
for (i = 0; i < height; i++) { /* Cr */
143
avio_write(pb, ptr2, width);
143
144
ptr2 += picture->linesize[2];
150
151
static int yuv4_write_header(AVFormatContext *s)
152
int* first_pkt = s->priv_data;
153
int *first_pkt = s->priv_data;
154
155
if (s->nb_streams != 1)
155
156
return AVERROR(EIO);
157
158
if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) {
158
av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n");
160
else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
161
(s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
162
(s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) &&
163
(s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
164
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");
159
av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV "
160
"stream, some mjpegtools might not work.\n");
161
} else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
162
(s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
163
(s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) &&
164
(s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) {
165
av_log(s, AV_LOG_ERROR, "ERROR: yuv4mpeg only handles yuv444p, "
166
"yuv422p, yuv420p, yuv411p and gray pixel formats. "
167
"Use -pix_fmt to select one.\n");
165
168
return AVERROR(EIO);
190
193
static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
192
char header[MAX_YUV4_HEADER+10]; // Include headroom for the longest option
193
char *tokstart,*tokend,*header_end;
195
char header[MAX_YUV4_HEADER + 10]; // Include headroom for
196
// the longest option
197
char *tokstart, *tokend, *header_end;
195
199
AVIOContext *pb = s->pb;
196
int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0;
197
enum PixelFormat pix_fmt=PIX_FMT_NONE,alt_pix_fmt=PIX_FMT_NONE;
200
int width = -1, height = -1, raten = 0,
201
rated = 0, aspectn = 0, aspectd = 0;
202
enum PixelFormat pix_fmt = PIX_FMT_NONE, alt_pix_fmt = PIX_FMT_NONE;
198
203
enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
200
205
struct frame_attributes *s1 = s->priv_data;
202
for (i=0; i<MAX_YUV4_HEADER; i++) {
207
for (i = 0; i < MAX_YUV4_HEADER; i++) {
203
208
header[i] = avio_r8(pb);
204
209
if (header[i] == '\n') {
205
header[i+1] = 0x20; // Add a space after last option. Makes parsing "444" vs "444alpha" easier.
210
header[i + 1] = 0x20; // Add a space after last option.
211
// Makes parsing "444" vs "444alpha" easier.
210
if (i == MAX_YUV4_HEADER) return -1;
211
if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1;
216
if (i == MAX_YUV4_HEADER)
218
if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC)))
213
221
s1->interlaced_frame = 0;
214
222
s1->top_field_first = 0;
215
header_end = &header[i+1]; // Include space
216
for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) {
217
if (*tokstart==0x20) continue;
223
header_end = &header[i + 1]; // Include space
224
for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
225
tokstart < header_end; tokstart++) {
226
if (*tokstart == 0x20)
218
228
switch (*tokstart++) {
219
229
case 'W': // Width. Required.
220
width = strtol(tokstart, &tokend, 10);
230
width = strtol(tokstart, &tokend, 10);
223
233
case 'H': // Height. Required.
224
height = strtol(tokstart, &tokend, 10);
234
height = strtol(tokstart, &tokend, 10);
227
237
case 'C': // Color space
228
if (strncmp("420jpeg",tokstart,7)==0) {
238
if (strncmp("420jpeg", tokstart, 7) == 0) {
229
239
pix_fmt = PIX_FMT_YUV420P;
230
240
chroma_sample_location = AVCHROMA_LOC_CENTER;
231
} else if (strncmp("420mpeg2",tokstart,8)==0) {
241
} else if (strncmp("420mpeg2", tokstart, 8) == 0) {
232
242
pix_fmt = PIX_FMT_YUV420P;
233
243
chroma_sample_location = AVCHROMA_LOC_LEFT;
234
} else if (strncmp("420paldv", tokstart, 8)==0) {
244
} else if (strncmp("420paldv", tokstart, 8) == 0) {
235
245
pix_fmt = PIX_FMT_YUV420P;
236
246
chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
237
} else if (strncmp("411", tokstart, 3)==0)
247
} else if (strncmp("411", tokstart, 3) == 0)
238
248
pix_fmt = PIX_FMT_YUV411P;
239
else if (strncmp("422", tokstart, 3)==0)
249
else if (strncmp("422", tokstart, 3) == 0)
240
250
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");
251
else if (strncmp("444alpha", tokstart, 8) == 0 ) {
252
av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 "
253
"YUV4MPEG stream.\n");
244
} else if (strncmp("444", tokstart, 3)==0)
255
} else if (strncmp("444", tokstart, 3) == 0)
245
256
pix_fmt = PIX_FMT_YUV444P;
246
else if (strncmp("mono",tokstart, 4)==0) {
257
else if (strncmp("mono", tokstart, 4) == 0) {
247
258
pix_fmt = PIX_FMT_GRAY8;
249
av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n");
260
av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
252
while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
264
while (tokstart < header_end && *tokstart != 0x20)
254
267
case 'I': // Interlace type
255
268
switch (*tokstart++){
259
s1->interlaced_frame=0;
272
s1->interlaced_frame = 0;
262
s1->interlaced_frame=1;
263
s1->top_field_first=1;
275
s1->interlaced_frame = 1;
276
s1->top_field_first = 1;
266
s1->interlaced_frame=1;
267
s1->top_field_first=0;
279
s1->interlaced_frame = 1;
280
s1->top_field_first = 0;
270
av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
283
av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
284
"interlaced and non-interlaced frames.\n");
273
287
av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
277
291
case 'F': // Frame rate
278
sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown
279
while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
292
sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
293
while (tokstart < header_end && *tokstart != 0x20)
281
296
case 'A': // Pixel aspect
282
sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown
283
while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
297
sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
298
while (tokstart < header_end && *tokstart != 0x20)
285
301
case 'X': // Vendor extensions
286
if (strncmp("YSCSS=",tokstart,6)==0) {
302
if (strncmp("YSCSS=", tokstart, 6) == 0) {
287
303
// Older nonstandard pixel format representation
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;
305
if (strncmp("420JPEG", tokstart, 7) == 0)
306
alt_pix_fmt = PIX_FMT_YUV420P;
307
else if (strncmp("420MPEG2", tokstart, 8) == 0)
308
alt_pix_fmt = PIX_FMT_YUV420P;
309
else if (strncmp("420PALDV", tokstart, 8) == 0)
310
alt_pix_fmt = PIX_FMT_YUV420P;
311
else if (strncmp("411", tokstart, 3) == 0)
312
alt_pix_fmt = PIX_FMT_YUV411P;
313
else if (strncmp("422", tokstart, 3) == 0)
314
alt_pix_fmt = PIX_FMT_YUV422P;
315
else if (strncmp("444", tokstart, 3) == 0)
316
alt_pix_fmt = PIX_FMT_YUV444P;
302
while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
318
while (tokstart < header_end && *tokstart != 0x20)
307
if ((width == -1) || (height == -1)) {
324
if (width == -1 || height == -1) {
308
325
av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
330
st = av_new_stream(s, 0);
347
st = avformat_new_stream(s, NULL);
332
349
return AVERROR(ENOMEM);
333
st->codec->width = width;
350
st->codec->width = width;
334
351
st->codec->height = height;
335
av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1);
336
av_set_pts_info(st, 64, rated, raten);
337
st->codec->pix_fmt = pix_fmt;
338
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
339
st->codec->codec_id = CODEC_ID_RAWVIDEO;
340
st->sample_aspect_ratio= (AVRational){aspectn, aspectd};
352
av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
353
avpriv_set_pts_info(st, 64, rated, raten);
354
st->codec->pix_fmt = pix_fmt;
355
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
356
st->codec->codec_id = CODEC_ID_RAWVIDEO;
357
st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
341
358
st->codec->chroma_sample_location = chroma_sample_location;