0b9fb89d |
/*
* YUV4MPEG format |
406792e7 |
* Copyright (c) 2001, 2002, 2003 Fabrice Bellard |
0b9fb89d |
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
0b9fb89d |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
0b9fb89d |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
0b9fb89d |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0b9fb89d |
*/
#include "avformat.h"
#define Y4M_MAGIC "YUV4MPEG2"
#define Y4M_FRAME_MAGIC "FRAME"
#define Y4M_LINE_MAX 256
|
ad97e414 |
struct frame_attributes {
int interlaced_frame;
int top_field_first;
};
|
b250f9c6 |
#if CONFIG_YUV4MPEGPIPE_MUXER |
2744ca9a |
static int yuv4_generate_header(AVFormatContext *s, char* buf) |
0b9fb89d |
{
AVStream *st;
int width, height; |
5ff85f1d |
int raten, rated, aspectn, aspectd, n; |
2744ca9a |
char inter; |
a97c1e93 |
const char *colorspace = ""; |
0b9fb89d |
st = s->streams[0]; |
01f4895c |
width = st->codec->width;
height = st->codec->height; |
14bea432 |
|
01f4895c |
av_reduce(&raten, &rated, st->codec->time_base.den, st->codec->time_base.num, (1UL<<31)-1); |
115329f1 |
|
59729451 |
aspectn = st->sample_aspect_ratio.num;
aspectd = st->sample_aspect_ratio.den; |
115329f1 |
|
2d745414 |
if ( aspectn == 0 && aspectd == 1 ) aspectd = 0; // 0:0 means unknown
|
2744ca9a |
inter = 'p'; /* progressive is the default */ |
01f4895c |
if (st->codec->coded_frame && st->codec->coded_frame->interlaced_frame) {
inter = st->codec->coded_frame->top_field_first ? 't' : 'b'; |
2744ca9a |
} |
0b9fb89d |
|
01f4895c |
switch(st->codec->pix_fmt) { |
f96b17c5 |
case PIX_FMT_GRAY8:
colorspace = " Cmono";
break; |
2d745414 |
case PIX_FMT_YUV411P:
colorspace = " C411 XYSCSS=411";
break;
case PIX_FMT_YUV420P: |
01f4895c |
colorspace = (st->codec->codec_id == CODEC_ID_DVVIDEO)?" C420paldv XYSCSS=420PALDV":" C420mpeg2 XYSCSS=420MPEG2"; |
2d745414 |
break;
case PIX_FMT_YUV422P:
colorspace = " C422 XYSCSS=422";
break;
case PIX_FMT_YUV444P:
colorspace = " C444 XYSCSS=444";
break;
}
|
0b9fb89d |
/* construct stream header, if this is the first frame */ |
2744ca9a |
n = snprintf(buf, Y4M_LINE_MAX, "%s W%d H%d F%d:%d I%c A%d:%d%s\n", |
0b9fb89d |
Y4M_MAGIC,
width,
height,
raten, rated, |
2744ca9a |
inter,
aspectn, aspectd, |
2d745414 |
colorspace); |
115329f1 |
|
2744ca9a |
return n; |
0b9fb89d |
}
|
e928649b |
static int yuv4_write_packet(AVFormatContext *s, AVPacket *pkt) |
0b9fb89d |
{ |
e928649b |
AVStream *st = s->streams[pkt->stream_index]; |
899681cd |
ByteIOContext *pb = s->pb; |
0b9fb89d |
AVPicture *picture; |
2744ca9a |
int* first_pkt = s->priv_data; |
2d745414 |
int width, height, h_chroma_shift, v_chroma_shift; |
0b9fb89d |
int i, m; |
2744ca9a |
char buf2[Y4M_LINE_MAX+1]; |
0b9fb89d |
char buf1[20]; |
0c1a9eda |
uint8_t *ptr, *ptr1, *ptr2; |
0b9fb89d |
|
e928649b |
picture = (AVPicture *)pkt->data; |
0b9fb89d |
|
2744ca9a |
/* for the first packet we have to output the header as well */
if (*first_pkt) {
*first_pkt = 0; |
bb270c08 |
if (yuv4_generate_header(s, buf2) < 0) {
av_log(s, AV_LOG_ERROR, "Error. YUV4MPEG stream header write failed.\n"); |
6f3e0b21 |
return AVERROR(EIO); |
bb270c08 |
} else {
put_buffer(pb, buf2, strlen(buf2));
} |
2744ca9a |
}
|
0b9fb89d |
/* construct frame header */ |
115329f1 |
|
a63d4933 |
m = snprintf(buf1, sizeof(buf1), "%s\n", Y4M_FRAME_MAGIC); |
0b9fb89d |
put_buffer(pb, buf1, strlen(buf1));
|
01f4895c |
width = st->codec->width;
height = st->codec->height; |
115329f1 |
|
0b9fb89d |
ptr = picture->data[0];
for(i=0;i<height;i++) {
put_buffer(pb, ptr, width);
ptr += picture->linesize[0];
}
|
01f4895c |
if (st->codec->pix_fmt != PIX_FMT_GRAY8){ |
2d745414 |
// Adjust for smaller Cb and Cr planes |
01f4895c |
avcodec_get_chroma_sub_sample(st->codec->pix_fmt, &h_chroma_shift, &v_chroma_shift); |
2d745414 |
width >>= h_chroma_shift;
height >>= v_chroma_shift;
|
0b9fb89d |
ptr1 = picture->data[1];
ptr2 = picture->data[2]; |
bb270c08 |
for(i=0;i<height;i++) { /* Cb */ |
0b9fb89d |
put_buffer(pb, ptr1, width);
ptr1 += picture->linesize[1];
} |
bb270c08 |
for(i=0;i<height;i++) { /* Cr */ |
0b9fb89d |
put_buffer(pb, ptr2, width);
ptr2 += picture->linesize[2];
} |
f96b17c5 |
} |
0b9fb89d |
put_flush_packet(pb);
return 0;
}
|
2744ca9a |
static int yuv4_write_header(AVFormatContext *s)
{
int* first_pkt = s->priv_data; |
115329f1 |
|
2744ca9a |
if (s->nb_streams != 1) |
6f3e0b21 |
return AVERROR(EIO); |
115329f1 |
|
01f4895c |
if (s->streams[0]->codec->pix_fmt == PIX_FMT_YUV411P) { |
2d745414 |
av_log(s, AV_LOG_ERROR, "Warning: generating rarely used 4:1:1 YUV stream, some mjpegtools might not work.\n"); |
115329f1 |
}
else if ((s->streams[0]->codec->pix_fmt != PIX_FMT_YUV420P) &&
(s->streams[0]->codec->pix_fmt != PIX_FMT_YUV422P) &&
(s->streams[0]->codec->pix_fmt != PIX_FMT_GRAY8) && |
01f4895c |
(s->streams[0]->codec->pix_fmt != PIX_FMT_YUV444P)) { |
f96b17c5 |
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"); |
6f3e0b21 |
return AVERROR(EIO); |
2744ca9a |
} |
115329f1 |
|
2744ca9a |
*first_pkt = 1;
return 0;
}
|
d2a067d1 |
AVOutputFormat yuv4mpegpipe_muxer = { |
0b9fb89d |
"yuv4mpegpipe", |
bde15e74 |
NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"), |
0b9fb89d |
"", |
8cf71ead |
"y4m", |
2744ca9a |
sizeof(int), |
0b9fb89d |
CODEC_ID_NONE,
CODEC_ID_RAWVIDEO,
yuv4_write_header,
yuv4_write_packet,
.flags = AVFMT_RAWPICTURE,
}; |
ff70e601 |
#endif |
0b9fb89d |
|
d9a916e2 |
/* Header size increased to allow room for optional flags */
#define MAX_YUV4_HEADER 80 |
2d745414 |
#define MAX_FRAME_HEADER 80 |
2864dfd5 |
static int yuv4_read_header(AVFormatContext *s, AVFormatParameters *ap)
{ |
2d745414 |
char header[MAX_YUV4_HEADER+10]; // Include headroom for the longest option
char *tokstart,*tokend,*header_end; |
2864dfd5 |
int i; |
899681cd |
ByteIOContext *pb = s->pb; |
ad97e414 |
int width=-1, height=-1, raten=0, rated=0, aspectn=0, aspectd=0; |
93b2b333 |
enum PixelFormat pix_fmt=PIX_FMT_NONE,alt_pix_fmt=PIX_FMT_NONE; |
2864dfd5 |
AVStream *st; |
ad97e414 |
struct frame_attributes *s1 = s->priv_data; |
115329f1 |
|
2864dfd5 |
for (i=0; i<MAX_YUV4_HEADER; i++) {
header[i] = get_byte(pb); |
bb270c08 |
if (header[i] == '\n') {
header[i+1] = 0x20; // Add a space after last option. Makes parsing "444" vs "444alpha" easier.
header[i+2] = 0;
break;
} |
2864dfd5 |
}
if (i == MAX_YUV4_HEADER) return -1;
if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) return -1; |
2d745414 |
|
ad97e414 |
s1->interlaced_frame = 0;
s1->top_field_first = 0; |
2d745414 |
header_end = &header[i+1]; // Include space
for(tokstart = &header[strlen(Y4M_MAGIC) + 1]; tokstart < header_end; tokstart++) {
if (*tokstart==0x20) continue;
switch (*tokstart++) {
case 'W': // Width. Required.
width = strtol(tokstart, &tokend, 10);
tokstart=tokend;
break;
case 'H': // Height. Required.
height = strtol(tokstart, &tokend, 10);
tokstart=tokend;
break;
case 'C': // Color space
if (strncmp("420jpeg",tokstart,7)==0)
pix_fmt = PIX_FMT_YUV420P;
else if (strncmp("420mpeg2",tokstart,8)==0)
pix_fmt = PIX_FMT_YUV420P;
else if (strncmp("420paldv", tokstart, 8)==0)
pix_fmt = PIX_FMT_YUV420P;
else if (strncmp("411", tokstart, 3)==0)
pix_fmt = PIX_FMT_YUV411P;
else if (strncmp("422", tokstart, 3)==0)
pix_fmt = PIX_FMT_YUV422P;
else if (strncmp("444alpha", tokstart, 8)==0) {
av_log(s, AV_LOG_ERROR, "Cannot handle 4:4:4:4 YUV4MPEG stream.\n");
return -1;
} else if (strncmp("444", tokstart, 3)==0)
pix_fmt = PIX_FMT_YUV444P;
else if (strncmp("mono",tokstart, 4)==0) { |
f96b17c5 |
pix_fmt = PIX_FMT_GRAY8; |
2d745414 |
} else {
av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown pixel format.\n");
return -1;
}
while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
break;
case 'I': // Interlace type
switch (*tokstart++){
case '?':
break;
case 'p': |
ad97e414 |
s1->interlaced_frame=0; |
2d745414 |
break;
case 't': |
ad97e414 |
s1->interlaced_frame=1;
s1->top_field_first=1; |
2d745414 |
break;
case 'b': |
ad97e414 |
s1->interlaced_frame=1;
s1->top_field_first=0; |
2d745414 |
break;
case 'm':
av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed interlaced and non-interlaced frames.\n");
return -1;
default:
av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
return -1;
}
break;
case 'F': // Frame rate
sscanf(tokstart,"%d:%d",&raten,&rated); // 0:0 if unknown
while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
break;
case 'A': // Pixel aspect
sscanf(tokstart,"%d:%d",&aspectn,&aspectd); // 0:0 if unknown
while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
break;
case 'X': // Vendor extensions
if (strncmp("YSCSS=",tokstart,6)==0) {
// Older nonstandard pixel format representation
tokstart+=6;
if (strncmp("420JPEG",tokstart,7)==0)
alt_pix_fmt=PIX_FMT_YUV420P;
else if (strncmp("420MPEG2",tokstart,8)==0)
alt_pix_fmt=PIX_FMT_YUV420P;
else if (strncmp("420PALDV",tokstart,8)==0)
alt_pix_fmt=PIX_FMT_YUV420P;
else if (strncmp("411",tokstart,3)==0)
alt_pix_fmt=PIX_FMT_YUV411P;
else if (strncmp("422",tokstart,3)==0)
alt_pix_fmt=PIX_FMT_YUV422P;
else if (strncmp("444",tokstart,3)==0)
alt_pix_fmt=PIX_FMT_YUV444P;
}
while(tokstart<header_end&&*tokstart!=0x20) tokstart++;
break;
} |
115329f1 |
} |
2d745414 |
if ((width == -1) || (height == -1)) {
av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n"); |
115329f1 |
return -1; |
2d745414 |
} |
115329f1 |
|
93b2b333 |
if (pix_fmt == PIX_FMT_NONE) {
if (alt_pix_fmt == PIX_FMT_NONE) |
2d745414 |
pix_fmt = PIX_FMT_YUV420P;
else
pix_fmt = alt_pix_fmt;
}
if (raten == 0 && rated == 0) {
// Frame rate unknown
raten = 25;
rated = 1;
}
if (aspectn == 0 && aspectd == 0) {
// Pixel aspect unknown
aspectd = 1;
} |
115329f1 |
|
2864dfd5 |
st = av_new_stream(s, 0); |
884d5385 |
if(!st) |
8b9c4ad2 |
return AVERROR(ENOMEM); |
01f4895c |
st->codec->width = width;
st->codec->height = height; |
2864dfd5 |
av_reduce(&raten, &rated, raten, rated, (1UL<<31)-1); |
c0df9d75 |
av_set_pts_info(st, 64, rated, raten); |
01f4895c |
st->codec->pix_fmt = pix_fmt;
st->codec->codec_type = CODEC_TYPE_VIDEO;
st->codec->codec_id = CODEC_ID_RAWVIDEO; |
59729451 |
st->sample_aspect_ratio= (AVRational){aspectn, aspectd}; |
2864dfd5 |
return 0;
}
static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
{
int i;
char header[MAX_FRAME_HEADER+1]; |
bf4e3bd2 |
int packet_size, width, height; |
2864dfd5 |
AVStream *st = s->streams[0]; |
ad97e414 |
struct frame_attributes *s1 = s->priv_data; |
2864dfd5 |
for (i=0; i<MAX_FRAME_HEADER; i++) { |
899681cd |
header[i] = get_byte(s->pb); |
bb270c08 |
if (header[i] == '\n') {
header[i+1] = 0;
break;
} |
2864dfd5 |
}
if (i == MAX_FRAME_HEADER) return -1;
if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC))) return -1; |
115329f1 |
|
01f4895c |
width = st->codec->width;
height = st->codec->height; |
2864dfd5 |
|
01f4895c |
packet_size = avpicture_get_size(st->codec->pix_fmt, width, height); |
2864dfd5 |
if (packet_size < 0) |
71c32f19 |
return -1; |
2864dfd5 |
|
899681cd |
if (av_get_packet(s->pb, pkt, packet_size) != packet_size) |
6f3e0b21 |
return AVERROR(EIO); |
2864dfd5 |
|
ad97e414 |
if (s->streams[0]->codec->coded_frame) {
s->streams[0]->codec->coded_frame->interlaced_frame = s1->interlaced_frame;
s->streams[0]->codec->coded_frame->top_field_first = s1->top_field_first;
}
|
2864dfd5 |
pkt->stream_index = 0; |
2692067a |
return 0; |
2864dfd5 |
}
|
8cf71ead |
static int yuv4_probe(AVProbeData *pd)
{
/* check file header */
if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC)-1)==0)
return AVPROBE_SCORE_MAX;
else
return 0;
}
|
b250f9c6 |
#if CONFIG_YUV4MPEGPIPE_DEMUXER |
d2a067d1 |
AVInputFormat yuv4mpegpipe_demuxer = { |
2864dfd5 |
"yuv4mpegpipe", |
bde15e74 |
NULL_IF_CONFIG_SMALL("YUV4MPEG pipe format"), |
ad97e414 |
sizeof(struct frame_attributes), |
8cf71ead |
yuv4_probe, |
2864dfd5 |
yuv4_read_header,
yuv4_read_packet, |
8cf71ead |
.extensions = "y4m" |
2864dfd5 |
}; |
ff70e601 |
#endif |