2006-03-27 22:22:50 +00:00
|
|
|
/*
|
|
|
|
* NuppelVideo demuxer.
|
2009-01-19 15:46:40 +00:00
|
|
|
* Copyright (c) 2006 Reimar Doeffinger
|
2006-03-27 22:22:50 +00:00
|
|
|
*
|
2006-10-07 15:30:46 +00:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2006-03-27 22:22:50 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 15:30:46 +00:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2006-03-27 22:22:50 +00:00
|
|
|
*
|
2006-10-07 15:30:46 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2006-03-27 22:22:50 +00: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 15:30:46 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-03-27 22:22:50 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2009-01-11 22:19:48 +00:00
|
|
|
|
|
|
|
#include "libavutil/intreadwrite.h"
|
2006-03-27 22:22:50 +00:00
|
|
|
#include "avformat.h"
|
2006-07-12 00:09:34 +00:00
|
|
|
#include "riff.h"
|
2006-03-27 22:22:50 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int v_id;
|
|
|
|
int a_id;
|
2007-09-09 07:25:52 +00:00
|
|
|
int rtjpg_video;
|
2006-03-27 22:22:50 +00:00
|
|
|
} NUVContext;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
NUV_VIDEO = 'V',
|
|
|
|
NUV_EXTRADATA = 'D',
|
|
|
|
NUV_AUDIO = 'A',
|
|
|
|
NUV_SEEKP = 'R',
|
|
|
|
NUV_MYTHEXT = 'X'
|
2008-12-11 19:19:23 +00:00
|
|
|
} nuv_frametype;
|
2006-03-27 22:22:50 +00:00
|
|
|
|
|
|
|
static int nuv_probe(AVProbeData *p) {
|
|
|
|
if (!memcmp(p->buf, "NuppelVideo", 12))
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
if (!memcmp(p->buf, "MythTVVideo", 12))
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! little macro to sanitize packet size
|
|
|
|
#define PKTSIZE(s) (s & 0xffffff)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief read until we found all data needed for decoding
|
|
|
|
* \param vst video stream of which to change parameters
|
|
|
|
* \param ast video stream of which to change parameters
|
|
|
|
* \param myth set if this is a MythTVVideo format file
|
|
|
|
* \return 1 if all required codec data was found
|
|
|
|
*/
|
|
|
|
static int get_codec_data(ByteIOContext *pb, AVStream *vst,
|
|
|
|
AVStream *ast, int myth) {
|
2008-12-11 19:19:23 +00:00
|
|
|
nuv_frametype frametype;
|
2006-03-27 22:22:50 +00:00
|
|
|
if (!vst && !myth)
|
|
|
|
return 1; // no codec data needed
|
|
|
|
while (!url_feof(pb)) {
|
|
|
|
int size, subtype;
|
|
|
|
frametype = get_byte(pb);
|
|
|
|
switch (frametype) {
|
|
|
|
case NUV_EXTRADATA:
|
|
|
|
subtype = get_byte(pb);
|
|
|
|
url_fskip(pb, 6);
|
|
|
|
size = PKTSIZE(get_le32(pb));
|
2006-04-20 10:38:15 +00:00
|
|
|
if (vst && subtype == 'R') {
|
2006-03-27 22:22:50 +00:00
|
|
|
vst->codec->extradata_size = size;
|
|
|
|
vst->codec->extradata = av_malloc(size);
|
|
|
|
get_buffer(pb, vst->codec->extradata, size);
|
|
|
|
size = 0;
|
|
|
|
if (!myth)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NUV_MYTHEXT:
|
|
|
|
url_fskip(pb, 7);
|
|
|
|
size = PKTSIZE(get_le32(pb));
|
|
|
|
if (size != 128 * 4)
|
|
|
|
break;
|
|
|
|
get_le32(pb); // version
|
|
|
|
if (vst) {
|
|
|
|
vst->codec->codec_tag = get_le32(pb);
|
|
|
|
vst->codec->codec_id =
|
2009-06-22 23:09:34 +00:00
|
|
|
ff_codec_get_id(ff_codec_bmp_tags, vst->codec->codec_tag);
|
2007-09-15 08:19:01 +00:00
|
|
|
if (vst->codec->codec_tag == MKTAG('R', 'J', 'P', 'G'))
|
|
|
|
vst->codec->codec_id = CODEC_ID_NUV;
|
2006-03-27 22:22:50 +00:00
|
|
|
} else
|
|
|
|
url_fskip(pb, 4);
|
|
|
|
|
|
|
|
if (ast) {
|
|
|
|
ast->codec->codec_tag = get_le32(pb);
|
|
|
|
ast->codec->sample_rate = get_le32(pb);
|
2008-09-08 14:24:59 +00:00
|
|
|
ast->codec->bits_per_coded_sample = get_le32(pb);
|
2006-03-27 22:22:50 +00:00
|
|
|
ast->codec->channels = get_le32(pb);
|
|
|
|
ast->codec->codec_id =
|
2009-06-22 23:09:34 +00:00
|
|
|
ff_wav_codec_get_id(ast->codec->codec_tag,
|
2008-09-08 14:24:59 +00:00
|
|
|
ast->codec->bits_per_coded_sample);
|
2007-05-27 08:06:45 +00:00
|
|
|
ast->need_parsing = AVSTREAM_PARSE_FULL;
|
2006-03-27 22:22:50 +00:00
|
|
|
} else
|
|
|
|
url_fskip(pb, 4 * 4);
|
|
|
|
|
|
|
|
size -= 6 * 4;
|
|
|
|
url_fskip(pb, size);
|
|
|
|
return 1;
|
|
|
|
case NUV_SEEKP:
|
|
|
|
size = 11;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
url_fskip(pb, 7);
|
|
|
|
size = PKTSIZE(get_le32(pb));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
url_fskip(pb, size);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nuv_header(AVFormatContext *s, AVFormatParameters *ap) {
|
2007-04-08 20:24:16 +00:00
|
|
|
NUVContext *ctx = s->priv_data;
|
2007-11-21 07:41:00 +00:00
|
|
|
ByteIOContext *pb = s->pb;
|
2009-04-07 21:16:11 +00:00
|
|
|
char id_string[12];
|
2006-03-27 22:22:50 +00:00
|
|
|
double aspect, fps;
|
|
|
|
int is_mythtv, width, height, v_packs, a_packs;
|
|
|
|
int stream_nr = 0;
|
|
|
|
AVStream *vst = NULL, *ast = NULL;
|
|
|
|
get_buffer(pb, id_string, 12);
|
|
|
|
is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
|
2009-04-07 21:16:11 +00:00
|
|
|
url_fskip(pb, 5); // version string
|
2006-03-27 22:22:50 +00:00
|
|
|
url_fskip(pb, 3); // padding
|
|
|
|
width = get_le32(pb);
|
|
|
|
height = get_le32(pb);
|
|
|
|
get_le32(pb); // unused, "desiredwidth"
|
|
|
|
get_le32(pb); // unused, "desiredheight"
|
|
|
|
get_byte(pb); // 'P' == progressive, 'I' == interlaced
|
|
|
|
url_fskip(pb, 3); // padding
|
|
|
|
aspect = av_int2dbl(get_le64(pb));
|
2008-07-25 04:10:32 +00:00
|
|
|
if (aspect > 0.9999 && aspect < 1.0001)
|
|
|
|
aspect = 4.0 / 3.0;
|
2006-03-27 22:22:50 +00:00
|
|
|
fps = av_int2dbl(get_le64(pb));
|
|
|
|
|
|
|
|
// number of packets per stream type, -1 means unknown, e.g. streaming
|
|
|
|
v_packs = get_le32(pb);
|
|
|
|
a_packs = get_le32(pb);
|
|
|
|
get_le32(pb); // text
|
|
|
|
|
|
|
|
get_le32(pb); // keyframe distance (?)
|
|
|
|
|
|
|
|
if (v_packs) {
|
|
|
|
ctx->v_id = stream_nr++;
|
|
|
|
vst = av_new_stream(s, ctx->v_id);
|
2008-05-29 15:25:45 +00:00
|
|
|
if (!vst)
|
|
|
|
return AVERROR(ENOMEM);
|
2010-03-30 23:30:55 +00:00
|
|
|
vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
|
2006-03-27 22:22:50 +00:00
|
|
|
vst->codec->codec_id = CODEC_ID_NUV;
|
|
|
|
vst->codec->width = width;
|
|
|
|
vst->codec->height = height;
|
2008-09-08 14:24:59 +00:00
|
|
|
vst->codec->bits_per_coded_sample = 10;
|
2008-08-23 23:43:20 +00:00
|
|
|
vst->sample_aspect_ratio = av_d2q(aspect * height / width, 10000);
|
2007-01-31 11:24:04 +00:00
|
|
|
vst->r_frame_rate = av_d2q(fps, 60000);
|
2006-03-27 22:22:50 +00:00
|
|
|
av_set_pts_info(vst, 32, 1, 1000);
|
|
|
|
} else
|
|
|
|
ctx->v_id = -1;
|
|
|
|
|
|
|
|
if (a_packs) {
|
|
|
|
ctx->a_id = stream_nr++;
|
|
|
|
ast = av_new_stream(s, ctx->a_id);
|
2008-05-29 15:25:45 +00:00
|
|
|
if (!ast)
|
|
|
|
return AVERROR(ENOMEM);
|
2010-03-30 23:30:55 +00:00
|
|
|
ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
|
2006-03-27 22:22:50 +00:00
|
|
|
ast->codec->codec_id = CODEC_ID_PCM_S16LE;
|
|
|
|
ast->codec->channels = 2;
|
|
|
|
ast->codec->sample_rate = 44100;
|
|
|
|
ast->codec->bit_rate = 2 * 2 * 44100 * 8;
|
|
|
|
ast->codec->block_align = 2 * 2;
|
2008-09-08 14:24:59 +00:00
|
|
|
ast->codec->bits_per_coded_sample = 16;
|
2006-03-27 22:22:50 +00:00
|
|
|
av_set_pts_info(ast, 32, 1, 1000);
|
|
|
|
} else
|
|
|
|
ctx->a_id = -1;
|
|
|
|
|
|
|
|
get_codec_data(pb, vst, ast, is_mythtv);
|
2008-07-07 16:30:39 +00:00
|
|
|
ctx->rtjpg_video = vst && vst->codec->codec_id == CODEC_ID_NUV;
|
2006-03-27 22:22:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define HDRSIZE 12
|
|
|
|
|
|
|
|
static int nuv_packet(AVFormatContext *s, AVPacket *pkt) {
|
2007-04-08 20:24:16 +00:00
|
|
|
NUVContext *ctx = s->priv_data;
|
2007-11-21 07:41:00 +00:00
|
|
|
ByteIOContext *pb = s->pb;
|
2006-03-27 22:22:50 +00:00
|
|
|
uint8_t hdr[HDRSIZE];
|
2008-12-11 19:19:23 +00:00
|
|
|
nuv_frametype frametype;
|
2006-03-27 22:22:50 +00:00
|
|
|
int ret, size;
|
|
|
|
while (!url_feof(pb)) {
|
2007-09-09 07:25:52 +00:00
|
|
|
int copyhdrsize = ctx->rtjpg_video ? HDRSIZE : 0;
|
2009-03-09 11:35:59 +00:00
|
|
|
uint64_t pos = url_ftell(pb);
|
2006-03-27 22:22:50 +00:00
|
|
|
ret = get_buffer(pb, hdr, HDRSIZE);
|
2009-04-07 20:52:37 +00:00
|
|
|
if (ret < HDRSIZE)
|
|
|
|
return ret < 0 ? ret : AVERROR(EIO);
|
2006-03-27 22:22:50 +00:00
|
|
|
frametype = hdr[0];
|
2007-01-19 22:12:59 +00:00
|
|
|
size = PKTSIZE(AV_RL32(&hdr[8]));
|
2006-03-27 22:22:50 +00:00
|
|
|
switch (frametype) {
|
|
|
|
case NUV_EXTRADATA:
|
2007-09-09 07:25:52 +00:00
|
|
|
if (!ctx->rtjpg_video) {
|
|
|
|
url_fskip(pb, size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NUV_VIDEO:
|
2006-03-27 22:22:50 +00:00
|
|
|
if (ctx->v_id < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
|
|
|
|
url_fskip(pb, size);
|
|
|
|
break;
|
|
|
|
}
|
2007-09-09 07:25:52 +00:00
|
|
|
ret = av_new_packet(pkt, copyhdrsize + size);
|
2006-03-27 22:22:50 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2009-03-09 12:25:13 +00:00
|
|
|
// HACK: we have no idea if it is a keyframe,
|
|
|
|
// but if we mark none seeking will not work at all.
|
2010-03-31 12:29:58 +00:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2009-03-09 11:35:59 +00:00
|
|
|
pkt->pos = pos;
|
2007-01-19 22:12:59 +00:00
|
|
|
pkt->pts = AV_RL32(&hdr[4]);
|
2006-03-27 22:22:50 +00:00
|
|
|
pkt->stream_index = ctx->v_id;
|
2007-09-09 07:25:52 +00:00
|
|
|
memcpy(pkt->data, hdr, copyhdrsize);
|
|
|
|
ret = get_buffer(pb, pkt->data + copyhdrsize, size);
|
2009-04-09 14:16:22 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
av_free_packet(pkt);
|
|
|
|
return ret;
|
|
|
|
}
|
2009-04-08 20:20:42 +00:00
|
|
|
if (ret < size)
|
|
|
|
av_shrink_packet(pkt, copyhdrsize + ret);
|
2009-04-07 21:25:02 +00:00
|
|
|
return 0;
|
2006-03-27 22:22:50 +00:00
|
|
|
case NUV_AUDIO:
|
|
|
|
if (ctx->a_id < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
|
|
|
|
url_fskip(pb, size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret = av_get_packet(pb, pkt, size);
|
2010-03-31 12:29:58 +00:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2009-03-09 11:35:59 +00:00
|
|
|
pkt->pos = pos;
|
2007-01-19 22:12:59 +00:00
|
|
|
pkt->pts = AV_RL32(&hdr[4]);
|
2006-03-27 22:22:50 +00:00
|
|
|
pkt->stream_index = ctx->a_id;
|
2009-04-07 21:25:02 +00:00
|
|
|
if (ret < 0) return ret;
|
|
|
|
return 0;
|
2006-03-27 22:22:50 +00:00
|
|
|
case NUV_SEEKP:
|
|
|
|
// contains no data, size value is invalid
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
url_fskip(pb, size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 15:23:32 +00:00
|
|
|
return AVERROR(EIO);
|
2006-03-27 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2006-07-10 21:14:37 +00:00
|
|
|
AVInputFormat nuv_demuxer = {
|
2006-03-27 22:22:50 +00:00
|
|
|
"nuv",
|
2008-06-03 16:20:54 +00:00
|
|
|
NULL_IF_CONFIG_SMALL("NuppelVideo format"),
|
2006-03-27 22:22:50 +00:00
|
|
|
sizeof(NUVContext),
|
|
|
|
nuv_probe,
|
|
|
|
nuv_header,
|
|
|
|
nuv_packet,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2009-03-14 22:07:15 +00:00
|
|
|
.flags = AVFMT_GENERIC_INDEX,
|
2006-03-27 22:22:50 +00:00
|
|
|
};
|