2003-01-11 06:58:36 +02:00
|
|
|
/*
|
2014-05-03 12:04:50 +03:00
|
|
|
* YUV4MPEG demuxer
|
2009-01-19 17:46:40 +02:00
|
|
|
* Copyright (c) 2001, 2002, 2003 Fabrice Bellard
|
2003-01-11 06:58:36 +02:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2003-01-11 06:58:36 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2003-01-11 06:58:36 +02:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2003-01-11 06:58:36 +02:00
|
|
|
* 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
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-13 00:43:26 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2003-01-11 06:58:36 +02:00
|
|
|
*/
|
2013-01-09 22:50:06 +03:00
|
|
|
|
2015-10-14 11:33:24 +02:00
|
|
|
#include "libavutil/imgutils.h"
|
|
|
|
|
2003-01-11 06:58:36 +02:00
|
|
|
#include "avformat.h"
|
2011-11-29 21:28:15 +03:00
|
|
|
#include "internal.h"
|
2014-05-03 12:04:50 +03:00
|
|
|
#include "yuv4mpeg.h"
|
2003-01-11 06:58:36 +02:00
|
|
|
|
2003-08-29 23:51:10 +03:00
|
|
|
/* Header size increased to allow room for optional flags */
|
2020-01-26 17:22:48 +02:00
|
|
|
#define MAX_YUV4_HEADER 96
|
2005-01-04 03:55:50 +02:00
|
|
|
#define MAX_FRAME_HEADER 80
|
2003-08-05 12:32:31 +03:00
|
|
|
|
2012-01-12 15:20:36 +03:00
|
|
|
static int yuv4_read_header(AVFormatContext *s)
|
2003-08-05 12:32:31 +03:00
|
|
|
{
|
2011-11-30 14:16:07 +03:00
|
|
|
char header[MAX_YUV4_HEADER + 10]; // Include headroom for
|
|
|
|
// the longest option
|
|
|
|
char *tokstart, *tokend, *header_end;
|
2003-08-05 12:32:31 +03:00
|
|
|
int i;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *pb = s->pb;
|
2011-11-30 14:16:07 +03:00
|
|
|
int width = -1, height = -1, raten = 0,
|
|
|
|
rated = 0, aspectn = 0, aspectd = 0;
|
2012-10-06 13:10:34 +03:00
|
|
|
enum AVPixelFormat pix_fmt = AV_PIX_FMT_NONE, alt_pix_fmt = AV_PIX_FMT_NONE;
|
2009-05-11 07:35:15 +03:00
|
|
|
enum AVChromaLocation chroma_sample_location = AVCHROMA_LOC_UNSPECIFIED;
|
2014-12-10 15:02:18 +02:00
|
|
|
enum AVFieldOrder field_order = AV_FIELD_UNKNOWN;
|
2018-06-28 10:32:01 +02:00
|
|
|
enum AVColorRange color_range = AVCOL_RANGE_UNSPECIFIED;
|
2003-08-05 12:32:31 +03:00
|
|
|
AVStream *st;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2011-11-30 14:16:07 +03:00
|
|
|
for (i = 0; i < MAX_YUV4_HEADER; i++) {
|
2011-02-21 17:43:01 +02:00
|
|
|
header[i] = avio_r8(pb);
|
2005-12-22 03:10:11 +02:00
|
|
|
if (header[i] == '\n') {
|
2011-11-30 14:16:07 +03:00
|
|
|
header[i + 1] = 0x20; // Add a space after last option.
|
|
|
|
// Makes parsing "444" vs "444alpha" easier.
|
|
|
|
header[i + 2] = 0;
|
2005-12-22 03:10:11 +02:00
|
|
|
break;
|
|
|
|
}
|
2003-08-05 12:32:31 +03:00
|
|
|
}
|
2020-01-26 17:20:30 +02:00
|
|
|
if (i == MAX_YUV4_HEADER) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Header too large.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
if (strncmp(header, Y4M_MAGIC, strlen(Y4M_MAGIC))) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Invalid magic number for yuv4mpeg.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2005-01-04 03:55:50 +02:00
|
|
|
|
2011-11-30 14:16:07 +03:00
|
|
|
header_end = &header[i + 1]; // Include space
|
|
|
|
for (tokstart = &header[strlen(Y4M_MAGIC) + 1];
|
|
|
|
tokstart < header_end; tokstart++) {
|
|
|
|
if (*tokstart == 0x20)
|
|
|
|
continue;
|
2005-01-04 03:55:50 +02:00
|
|
|
switch (*tokstart++) {
|
|
|
|
case 'W': // Width. Required.
|
2011-11-30 14:16:07 +03:00
|
|
|
width = strtol(tokstart, &tokend, 10);
|
|
|
|
tokstart = tokend;
|
2005-01-04 03:55:50 +02:00
|
|
|
break;
|
|
|
|
case 'H': // Height. Required.
|
2011-11-30 14:16:07 +03:00
|
|
|
height = strtol(tokstart, &tokend, 10);
|
|
|
|
tokstart = tokend;
|
2005-01-04 03:55:50 +02:00
|
|
|
break;
|
|
|
|
case 'C': // Color space
|
2011-11-30 14:16:07 +03:00
|
|
|
if (strncmp("420jpeg", tokstart, 7) == 0) {
|
2012-10-06 13:10:34 +03:00
|
|
|
pix_fmt = AV_PIX_FMT_YUV420P;
|
2009-05-11 07:35:15 +03:00
|
|
|
chroma_sample_location = AVCHROMA_LOC_CENTER;
|
2011-11-30 14:16:07 +03:00
|
|
|
} else if (strncmp("420mpeg2", tokstart, 8) == 0) {
|
2012-10-06 13:10:34 +03:00
|
|
|
pix_fmt = AV_PIX_FMT_YUV420P;
|
2009-05-11 07:35:15 +03:00
|
|
|
chroma_sample_location = AVCHROMA_LOC_LEFT;
|
2011-11-30 14:16:07 +03:00
|
|
|
} else if (strncmp("420paldv", tokstart, 8) == 0) {
|
2012-10-06 13:10:34 +03:00
|
|
|
pix_fmt = AV_PIX_FMT_YUV420P;
|
2009-05-11 07:35:15 +03:00
|
|
|
chroma_sample_location = AVCHROMA_LOC_TOPLEFT;
|
2012-05-08 00:14:27 +03:00
|
|
|
} else if (strncmp("420p16", tokstart, 6) == 0) {
|
2012-10-08 21:54:00 +03:00
|
|
|
pix_fmt = AV_PIX_FMT_YUV420P16;
|
2012-05-08 00:14:27 +03:00
|
|
|
} else if (strncmp("422p16", tokstart, 6) == 0) {
|
2012-10-08 21:54:00 +03:00
|
|
|
pix_fmt = AV_PIX_FMT_YUV422P16;
|
2012-05-08 00:14:27 +03:00
|
|
|
} else if (strncmp("444p16", tokstart, 6) == 0) {
|
2012-10-08 21:54:00 +03:00
|
|
|
pix_fmt = AV_PIX_FMT_YUV444P16;
|
2012-11-10 04:19:55 +03:00
|
|
|
} else if (strncmp("420p14", tokstart, 6) == 0) {
|
|
|
|
pix_fmt = AV_PIX_FMT_YUV420P14;
|
|
|
|
} else if (strncmp("422p14", tokstart, 6) == 0) {
|
|
|
|
pix_fmt = AV_PIX_FMT_YUV422P14;
|
|
|
|
} else if (strncmp("444p14", tokstart, 6) == 0) {
|
|
|
|
pix_fmt = AV_PIX_FMT_YUV444P14;
|
|
|
|
} else if (strncmp("420p12", tokstart, 6) == 0) {
|
|
|
|
pix_fmt = AV_PIX_FMT_YUV420P12;
|
|
|
|
} else if (strncmp("422p12", tokstart, 6) == 0) {
|
|
|
|
pix_fmt = AV_PIX_FMT_YUV422P12;
|
|
|
|
} else if (strncmp("444p12", tokstart, 6) == 0) {
|
|
|
|
pix_fmt = AV_PIX_FMT_YUV444P12;
|
2012-05-08 00:14:27 +03:00
|
|
|
} else if (strncmp("420p10", tokstart, 6) == 0) {
|
2012-10-08 21:54:00 +03:00
|
|
|
pix_fmt = AV_PIX_FMT_YUV420P10;
|
2012-05-08 00:14:27 +03:00
|
|
|
} else if (strncmp("422p10", tokstart, 6) == 0) {
|
2012-10-08 21:54:00 +03:00
|
|
|
pix_fmt = AV_PIX_FMT_YUV422P10;
|
2012-05-08 00:14:27 +03:00
|
|
|
} else if (strncmp("444p10", tokstart, 6) == 0) {
|
2012-10-08 21:54:00 +03:00
|
|
|
pix_fmt = AV_PIX_FMT_YUV444P10;
|
2012-05-08 00:14:27 +03:00
|
|
|
} else if (strncmp("420p9", tokstart, 5) == 0) {
|
2012-10-08 21:54:00 +03:00
|
|
|
pix_fmt = AV_PIX_FMT_YUV420P9;
|
2012-05-08 00:14:27 +03:00
|
|
|
} else if (strncmp("422p9", tokstart, 5) == 0) {
|
2012-10-08 21:54:00 +03:00
|
|
|
pix_fmt = AV_PIX_FMT_YUV422P9;
|
2012-05-08 00:14:27 +03:00
|
|
|
} else if (strncmp("444p9", tokstart, 5) == 0) {
|
2012-10-08 21:54:00 +03:00
|
|
|
pix_fmt = AV_PIX_FMT_YUV444P9;
|
2012-01-30 08:24:41 +03:00
|
|
|
} else if (strncmp("420", tokstart, 3) == 0) {
|
2012-10-06 13:10:34 +03:00
|
|
|
pix_fmt = AV_PIX_FMT_YUV420P;
|
2012-01-30 08:24:41 +03:00
|
|
|
chroma_sample_location = AVCHROMA_LOC_CENTER;
|
2012-05-08 00:14:27 +03:00
|
|
|
} else if (strncmp("411", tokstart, 3) == 0) {
|
2012-10-06 13:10:34 +03:00
|
|
|
pix_fmt = AV_PIX_FMT_YUV411P;
|
2012-05-08 00:14:27 +03:00
|
|
|
} else if (strncmp("422", tokstart, 3) == 0) {
|
2012-10-06 13:10:34 +03:00
|
|
|
pix_fmt = AV_PIX_FMT_YUV422P;
|
2012-05-08 00:14:27 +03:00
|
|
|
} else if (strncmp("444alpha", tokstart, 8) == 0 ) {
|
2020-03-19 12:11:24 +02:00
|
|
|
pix_fmt = AV_PIX_FMT_YUVA444P;
|
2012-05-08 00:14:27 +03:00
|
|
|
} else if (strncmp("444", tokstart, 3) == 0) {
|
2012-10-06 13:10:34 +03:00
|
|
|
pix_fmt = AV_PIX_FMT_YUV444P;
|
2012-05-08 00:14:27 +03:00
|
|
|
} else if (strncmp("mono16", tokstart, 6) == 0) {
|
2012-10-08 21:54:00 +03:00
|
|
|
pix_fmt = AV_PIX_FMT_GRAY16;
|
2017-10-07 19:05:53 +02:00
|
|
|
} else if (strncmp("mono12", tokstart, 6) == 0) {
|
|
|
|
pix_fmt = AV_PIX_FMT_GRAY12;
|
|
|
|
} else if (strncmp("mono10", tokstart, 6) == 0) {
|
|
|
|
pix_fmt = AV_PIX_FMT_GRAY10;
|
|
|
|
} else if (strncmp("mono9", tokstart, 5) == 0) {
|
|
|
|
pix_fmt = AV_PIX_FMT_GRAY9;
|
2012-07-22 10:59:25 +03:00
|
|
|
} else if (strncmp("mono", tokstart, 4) == 0) {
|
2012-10-06 13:10:34 +03:00
|
|
|
pix_fmt = AV_PIX_FMT_GRAY8;
|
2005-01-04 03:55:50 +02:00
|
|
|
} else {
|
2011-11-30 14:16:07 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains an unknown "
|
|
|
|
"pixel format.\n");
|
2020-03-19 12:08:42 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2005-01-04 03:55:50 +02:00
|
|
|
}
|
2011-11-30 14:16:07 +03:00
|
|
|
while (tokstart < header_end && *tokstart != 0x20)
|
|
|
|
tokstart++;
|
2005-01-04 03:55:50 +02:00
|
|
|
break;
|
|
|
|
case 'I': // Interlace type
|
|
|
|
switch (*tokstart++){
|
|
|
|
case '?':
|
2014-05-03 12:08:25 +03:00
|
|
|
field_order = AV_FIELD_UNKNOWN;
|
2005-01-04 03:55:50 +02:00
|
|
|
break;
|
|
|
|
case 'p':
|
2014-05-03 12:08:25 +03:00
|
|
|
field_order = AV_FIELD_PROGRESSIVE;
|
2005-01-04 03:55:50 +02:00
|
|
|
break;
|
|
|
|
case 't':
|
2014-05-03 12:08:25 +03:00
|
|
|
field_order = AV_FIELD_TT;
|
2005-01-04 03:55:50 +02:00
|
|
|
break;
|
|
|
|
case 'b':
|
2014-05-03 12:08:25 +03:00
|
|
|
field_order = AV_FIELD_BB;
|
2005-01-04 03:55:50 +02:00
|
|
|
break;
|
|
|
|
case 'm':
|
2011-11-30 14:16:07 +03:00
|
|
|
av_log(s, AV_LOG_ERROR, "YUV4MPEG stream contains mixed "
|
|
|
|
"interlaced and non-interlaced frames.\n");
|
2005-01-04 03:55:50 +02:00
|
|
|
default:
|
|
|
|
av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
|
2014-05-18 15:02:31 +03:00
|
|
|
return AVERROR(EINVAL);
|
2005-01-04 03:55:50 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'F': // Frame rate
|
2011-11-30 14:16:07 +03:00
|
|
|
sscanf(tokstart, "%d:%d", &raten, &rated); // 0:0 if unknown
|
|
|
|
while (tokstart < header_end && *tokstart != 0x20)
|
|
|
|
tokstart++;
|
2005-01-04 03:55:50 +02:00
|
|
|
break;
|
|
|
|
case 'A': // Pixel aspect
|
2011-11-30 14:16:07 +03:00
|
|
|
sscanf(tokstart, "%d:%d", &aspectn, &aspectd); // 0:0 if unknown
|
|
|
|
while (tokstart < header_end && *tokstart != 0x20)
|
|
|
|
tokstart++;
|
2005-01-04 03:55:50 +02:00
|
|
|
break;
|
|
|
|
case 'X': // Vendor extensions
|
2011-11-30 14:16:07 +03:00
|
|
|
if (strncmp("YSCSS=", tokstart, 6) == 0) {
|
2005-01-04 03:55:50 +02:00
|
|
|
// Older nonstandard pixel format representation
|
2011-11-30 14:16:07 +03:00
|
|
|
tokstart += 6;
|
|
|
|
if (strncmp("420JPEG", tokstart, 7) == 0)
|
2012-10-06 13:10:34 +03:00
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV420P;
|
2011-11-30 14:16:07 +03:00
|
|
|
else if (strncmp("420MPEG2", tokstart, 8) == 0)
|
2012-10-06 13:10:34 +03:00
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV420P;
|
2011-11-30 14:16:07 +03:00
|
|
|
else if (strncmp("420PALDV", tokstart, 8) == 0)
|
2012-10-06 13:10:34 +03:00
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV420P;
|
2012-05-08 00:14:27 +03:00
|
|
|
else if (strncmp("420P9", tokstart, 5) == 0)
|
2012-10-08 21:54:00 +03:00
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV420P9;
|
2012-05-08 00:14:27 +03:00
|
|
|
else if (strncmp("422P9", tokstart, 5) == 0)
|
2012-10-08 21:54:00 +03:00
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV422P9;
|
2012-05-08 00:14:27 +03:00
|
|
|
else if (strncmp("444P9", tokstart, 5) == 0)
|
2012-10-08 21:54:00 +03:00
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV444P9;
|
2012-05-08 00:14:27 +03:00
|
|
|
else if (strncmp("420P10", tokstart, 6) == 0)
|
2012-10-08 21:54:00 +03:00
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV420P10;
|
2012-05-08 00:14:27 +03:00
|
|
|
else if (strncmp("422P10", tokstart, 6) == 0)
|
2012-10-08 21:54:00 +03:00
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV422P10;
|
2012-05-08 00:14:27 +03:00
|
|
|
else if (strncmp("444P10", tokstart, 6) == 0)
|
2012-10-08 21:54:00 +03:00
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV444P10;
|
2012-11-10 04:19:55 +03:00
|
|
|
else if (strncmp("420P12", tokstart, 6) == 0)
|
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV420P12;
|
|
|
|
else if (strncmp("422P12", tokstart, 6) == 0)
|
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV422P12;
|
|
|
|
else if (strncmp("444P12", tokstart, 6) == 0)
|
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV444P12;
|
|
|
|
else if (strncmp("420P14", tokstart, 6) == 0)
|
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV420P14;
|
|
|
|
else if (strncmp("422P14", tokstart, 6) == 0)
|
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV422P14;
|
|
|
|
else if (strncmp("444P14", tokstart, 6) == 0)
|
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV444P14;
|
2012-05-08 00:14:27 +03:00
|
|
|
else if (strncmp("420P16", tokstart, 6) == 0)
|
2012-10-08 21:54:00 +03:00
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV420P16;
|
2012-05-08 00:14:27 +03:00
|
|
|
else if (strncmp("422P16", tokstart, 6) == 0)
|
2012-10-08 21:54:00 +03:00
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV422P16;
|
2012-05-08 00:14:27 +03:00
|
|
|
else if (strncmp("444P16", tokstart, 6) == 0)
|
2012-10-08 21:54:00 +03:00
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV444P16;
|
2011-11-30 14:16:07 +03:00
|
|
|
else if (strncmp("411", tokstart, 3) == 0)
|
2012-10-06 13:10:34 +03:00
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV411P;
|
2011-11-30 14:16:07 +03:00
|
|
|
else if (strncmp("422", tokstart, 3) == 0)
|
2012-10-06 13:10:34 +03:00
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV422P;
|
2011-11-30 14:16:07 +03:00
|
|
|
else if (strncmp("444", tokstart, 3) == 0)
|
2012-10-06 13:10:34 +03:00
|
|
|
alt_pix_fmt = AV_PIX_FMT_YUV444P;
|
2018-06-28 10:32:01 +02:00
|
|
|
} else if (strncmp("COLORRANGE=", tokstart, 11) == 0) {
|
|
|
|
tokstart += 11;
|
|
|
|
if (strncmp("FULL",tokstart, 4) == 0)
|
|
|
|
color_range = AVCOL_RANGE_JPEG;
|
|
|
|
else if (strncmp("LIMITED", tokstart, 7) == 0)
|
|
|
|
color_range = AVCOL_RANGE_MPEG;
|
2005-01-04 03:55:50 +02:00
|
|
|
}
|
2011-11-30 14:16:07 +03:00
|
|
|
while (tokstart < header_end && *tokstart != 0x20)
|
|
|
|
tokstart++;
|
2005-01-04 03:55:50 +02:00
|
|
|
break;
|
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
}
|
2005-01-04 03:55:50 +02:00
|
|
|
|
2011-11-30 14:16:07 +03:00
|
|
|
if (width == -1 || height == -1) {
|
2005-01-04 03:55:50 +02:00
|
|
|
av_log(s, AV_LOG_ERROR, "YUV4MPEG has invalid header.\n");
|
2020-03-19 12:08:42 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2005-01-04 03:55:50 +02:00
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2012-10-06 13:10:34 +03:00
|
|
|
if (pix_fmt == AV_PIX_FMT_NONE) {
|
|
|
|
if (alt_pix_fmt == AV_PIX_FMT_NONE)
|
|
|
|
pix_fmt = AV_PIX_FMT_YUV420P;
|
2005-01-04 03:55:50 +02:00
|
|
|
else
|
|
|
|
pix_fmt = alt_pix_fmt;
|
|
|
|
}
|
|
|
|
|
2011-01-02 07:01:46 +02:00
|
|
|
if (raten <= 0 || rated <= 0) {
|
2005-01-04 03:55:50 +02:00
|
|
|
// Frame rate unknown
|
|
|
|
raten = 25;
|
|
|
|
rated = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aspectn == 0 && aspectd == 0) {
|
|
|
|
// Pixel aspect unknown
|
|
|
|
aspectd = 1;
|
|
|
|
}
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2011-06-18 12:43:24 +03:00
|
|
|
st = avformat_new_stream(s, NULL);
|
2011-11-30 14:16:07 +03:00
|
|
|
if (!st)
|
2008-05-29 18:40:16 +03:00
|
|
|
return AVERROR(ENOMEM);
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
st->codecpar->width = width;
|
|
|
|
st->codecpar->height = height;
|
2011-11-30 14:16:07 +03:00
|
|
|
av_reduce(&raten, &rated, raten, rated, (1UL << 31) - 1);
|
2011-11-29 21:28:15 +03:00
|
|
|
avpriv_set_pts_info(st, 64, rated, raten);
|
2014-01-15 23:14:46 +03:00
|
|
|
st->avg_frame_rate = av_inv_q(st->time_base);
|
2016-04-10 21:58:15 +02:00
|
|
|
st->codecpar->format = pix_fmt;
|
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
|
2011-11-30 14:16:07 +03:00
|
|
|
st->sample_aspect_ratio = (AVRational){ aspectn, aspectd };
|
2016-04-10 21:58:15 +02:00
|
|
|
st->codecpar->chroma_location = chroma_sample_location;
|
2018-06-28 10:32:01 +02:00
|
|
|
st->codecpar->color_range = color_range;
|
2016-04-10 21:58:15 +02:00
|
|
|
st->codecpar->field_order = field_order;
|
|
|
|
s->packet_size = av_image_get_buffer_size(st->codecpar->format, width, height, 1) + Y4M_FRAME_MAGIC_LEN;
|
2015-07-11 03:15:35 +02:00
|
|
|
if ((int) s->packet_size < 0)
|
|
|
|
return s->packet_size;
|
|
|
|
s->internal->data_offset = avio_tell(pb);
|
|
|
|
|
|
|
|
st->duration = (avio_size(pb) - avio_tell(pb)) / s->packet_size;
|
2013-02-02 14:29:58 +03:00
|
|
|
|
2003-08-05 12:32:31 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int yuv4_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char header[MAX_FRAME_HEADER+1];
|
2015-07-12 00:00:21 +02:00
|
|
|
int ret;
|
|
|
|
int64_t off = avio_tell(s->pb);
|
2003-08-05 12:32:31 +03:00
|
|
|
|
2011-11-30 14:16:07 +03:00
|
|
|
for (i = 0; i < MAX_FRAME_HEADER; i++) {
|
2011-02-21 17:43:01 +02:00
|
|
|
header[i] = avio_r8(s->pb);
|
2005-12-22 03:10:11 +02:00
|
|
|
if (header[i] == '\n') {
|
2011-11-30 14:16:07 +03:00
|
|
|
header[i + 1] = 0;
|
2005-12-22 03:10:11 +02:00
|
|
|
break;
|
|
|
|
}
|
2003-08-05 12:32:31 +03:00
|
|
|
}
|
2012-10-05 16:53:32 +03:00
|
|
|
if (s->pb->error)
|
|
|
|
return s->pb->error;
|
|
|
|
else if (s->pb->eof_reached)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
else if (i == MAX_FRAME_HEADER)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2011-11-30 14:16:07 +03:00
|
|
|
if (strncmp(header, Y4M_FRAME_MAGIC, strlen(Y4M_FRAME_MAGIC)))
|
2012-10-05 16:53:32 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
2005-12-17 20:14:38 +02:00
|
|
|
|
2015-07-11 03:15:35 +02:00
|
|
|
ret = av_get_packet(s->pb, pkt, s->packet_size - Y4M_FRAME_MAGIC_LEN);
|
2012-10-05 16:53:32 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2016-12-05 23:56:36 +02:00
|
|
|
else if (ret != s->packet_size - Y4M_FRAME_MAGIC_LEN) {
|
2012-10-05 16:53:32 +03:00
|
|
|
return s->pb->eof_reached ? AVERROR_EOF : AVERROR(EIO);
|
2016-12-05 23:56:36 +02:00
|
|
|
}
|
2003-08-05 12:32:31 +03:00
|
|
|
pkt->stream_index = 0;
|
2015-07-11 03:15:35 +02:00
|
|
|
pkt->pts = (off - s->internal->data_offset) / s->packet_size;
|
|
|
|
pkt->duration = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int yuv4_read_seek(AVFormatContext *s, int stream_index,
|
|
|
|
int64_t pts, int flags)
|
|
|
|
{
|
2018-04-25 18:30:46 +02:00
|
|
|
int64_t pos;
|
|
|
|
|
2018-05-01 17:46:08 +02:00
|
|
|
if (flags & AVSEEK_FLAG_BACKWARD)
|
|
|
|
pts = FFMAX(0, pts - 1);
|
2019-09-22 19:24:17 +02:00
|
|
|
if (pts < 0)
|
|
|
|
return -1;
|
2018-05-01 17:46:08 +02:00
|
|
|
pos = pts * s->packet_size;
|
2018-04-25 18:30:46 +02:00
|
|
|
|
|
|
|
if (avio_seek(s->pb, pos + s->internal->data_offset, SEEK_SET) < 0)
|
2016-03-02 21:56:01 +02:00
|
|
|
return -1;
|
2005-05-26 23:17:12 +03:00
|
|
|
return 0;
|
2003-08-05 12:32:31 +03:00
|
|
|
}
|
|
|
|
|
2019-03-21 02:18:37 +02:00
|
|
|
static int yuv4_probe(const AVProbeData *pd)
|
2005-04-26 12:06:39 +03:00
|
|
|
{
|
|
|
|
/* check file header */
|
2011-11-30 14:16:07 +03:00
|
|
|
if (strncmp(pd->buf, Y4M_MAGIC, sizeof(Y4M_MAGIC) - 1) == 0)
|
2005-04-26 12:06:39 +03:00
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-26 00:03:28 +02:00
|
|
|
AVInputFormat ff_yuv4mpegpipe_demuxer = {
|
2011-07-16 23:18:12 +03:00
|
|
|
.name = "yuv4mpegpipe",
|
2012-07-25 00:51:41 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("YUV4MPEG pipe"),
|
2011-07-16 23:18:12 +03:00
|
|
|
.read_probe = yuv4_probe,
|
|
|
|
.read_header = yuv4_read_header,
|
|
|
|
.read_packet = yuv4_read_packet,
|
2015-07-11 03:15:35 +02:00
|
|
|
.read_seek = yuv4_read_seek,
|
2012-04-06 17:50:48 +03:00
|
|
|
.extensions = "y4m",
|
2003-08-05 12:32:31 +03:00
|
|
|
};
|