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
|
|
|
|
*/
|
2011-02-20 10:04:12 +00:00
|
|
|
static int get_codec_data(AVIOContext *pb, AVStream *vst,
|
2006-03-27 22:22:50 +00:00
|
|
|
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;
|
2011-02-21 15:43:01 +00:00
|
|
|
frametype = avio_r8(pb);
|
2006-03-27 22:22:50 +00:00
|
|
|
switch (frametype) {
|
|
|
|
case NUV_EXTRADATA:
|
2011-02-21 15:43:01 +00:00
|
|
|
subtype = avio_r8(pb);
|
2011-02-28 13:57:55 +00:00
|
|
|
avio_seek(pb, 6, SEEK_CUR);
|
2011-02-21 15:43:01 +00:00
|
|
|
size = PKTSIZE(avio_rl32(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);
|
2011-02-21 15:43:01 +00:00
|
|
|
avio_read(pb, vst->codec->extradata, size);
|
2006-03-27 22:22:50 +00:00
|
|
|
size = 0;
|
|
|
|
if (!myth)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NUV_MYTHEXT:
|
2011-02-28 13:57:55 +00:00
|
|
|
avio_seek(pb, 7, SEEK_CUR);
|
2011-02-21 15:43:01 +00:00
|
|
|
size = PKTSIZE(avio_rl32(pb));
|
2006-03-27 22:22:50 +00:00
|
|
|
if (size != 128 * 4)
|
|
|
|
break;
|
2011-02-21 15:43:01 +00:00
|
|
|
avio_rl32(pb); // version
|
2006-03-27 22:22:50 +00:00
|
|
|
if (vst) {
|
2011-02-21 15:43:01 +00:00
|
|
|
vst->codec->codec_tag = avio_rl32(pb);
|
2006-03-27 22:22:50 +00:00
|
|
|
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
|
2011-02-28 13:57:55 +00:00
|
|
|
avio_seek(pb, 4, SEEK_CUR);
|
2006-03-27 22:22:50 +00:00
|
|
|
|
|
|
|
if (ast) {
|
2011-02-21 15:43:01 +00:00
|
|
|
ast->codec->codec_tag = avio_rl32(pb);
|
|
|
|
ast->codec->sample_rate = avio_rl32(pb);
|
|
|
|
ast->codec->bits_per_coded_sample = avio_rl32(pb);
|
|
|
|
ast->codec->channels = avio_rl32(pb);
|
2006-03-27 22:22:50 +00:00
|
|
|
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
|
2011-02-28 13:57:55 +00:00
|
|
|
avio_seek(pb, 4 * 4, SEEK_CUR);
|
2006-03-27 22:22:50 +00:00
|
|
|
|
|
|
|
size -= 6 * 4;
|
2011-02-28 13:57:55 +00:00
|
|
|
avio_seek(pb, size, SEEK_CUR);
|
2006-03-27 22:22:50 +00:00
|
|
|
return 1;
|
|
|
|
case NUV_SEEKP:
|
|
|
|
size = 11;
|
|
|
|
break;
|
|
|
|
default:
|
2011-02-28 13:57:55 +00:00
|
|
|
avio_seek(pb, 7, SEEK_CUR);
|
2011-02-21 15:43:01 +00:00
|
|
|
size = PKTSIZE(avio_rl32(pb));
|
2006-03-27 22:22:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-02-28 13:57:55 +00:00
|
|
|
avio_seek(pb, size, SEEK_CUR);
|
2006-03-27 22:22:50 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nuv_header(AVFormatContext *s, AVFormatParameters *ap) {
|
2007-04-08 20:24:16 +00:00
|
|
|
NUVContext *ctx = s->priv_data;
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *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;
|
2011-02-21 15:43:01 +00:00
|
|
|
avio_read(pb, id_string, 12);
|
2006-03-27 22:22:50 +00:00
|
|
|
is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
|
2011-02-28 13:57:55 +00:00
|
|
|
avio_seek(pb, 5, SEEK_CUR); // version string
|
|
|
|
avio_seek(pb, 3, SEEK_CUR); // padding
|
2011-02-21 15:43:01 +00:00
|
|
|
width = avio_rl32(pb);
|
|
|
|
height = avio_rl32(pb);
|
|
|
|
avio_rl32(pb); // unused, "desiredwidth"
|
|
|
|
avio_rl32(pb); // unused, "desiredheight"
|
|
|
|
avio_r8(pb); // 'P' == progressive, 'I' == interlaced
|
2011-02-28 13:57:55 +00:00
|
|
|
avio_seek(pb, 3, SEEK_CUR); // padding
|
2011-02-21 15:43:01 +00:00
|
|
|
aspect = av_int2dbl(avio_rl64(pb));
|
2008-07-25 04:10:32 +00:00
|
|
|
if (aspect > 0.9999 && aspect < 1.0001)
|
|
|
|
aspect = 4.0 / 3.0;
|
2011-02-21 15:43:01 +00:00
|
|
|
fps = av_int2dbl(avio_rl64(pb));
|
2006-03-27 22:22:50 +00:00
|
|
|
|
|
|
|
// number of packets per stream type, -1 means unknown, e.g. streaming
|
2011-02-21 15:43:01 +00:00
|
|
|
v_packs = avio_rl32(pb);
|
|
|
|
a_packs = avio_rl32(pb);
|
|
|
|
avio_rl32(pb); // text
|
2006-03-27 22:22:50 +00:00
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
avio_rl32(pb); // keyframe distance (?)
|
2006-03-27 22:22:50 +00:00
|
|
|
|
|
|
|
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;
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *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;
|
2011-03-03 19:11:45 +00:00
|
|
|
uint64_t pos = avio_tell(pb);
|
2011-02-21 15:43:01 +00:00
|
|
|
ret = avio_read(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) {
|
2011-02-28 13:57:55 +00:00
|
|
|
avio_seek(pb, size, SEEK_CUR);
|
2007-09-09 07:25:52 +00:00
|
|
|
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");
|
2011-02-28 13:57:55 +00:00
|
|
|
avio_seek(pb, size, SEEK_CUR);
|
2006-03-27 22:22:50 +00:00
|
|
|
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);
|
2011-02-21 15:43:01 +00:00
|
|
|
ret = avio_read(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");
|
2011-02-28 13:57:55 +00:00
|
|
|
avio_seek(pb, size, SEEK_CUR);
|
2006-03-27 22:22:50 +00:00
|
|
|
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:
|
2011-02-28 13:57:55 +00:00
|
|
|
avio_seek(pb, size, SEEK_CUR);
|
2006-03-27 22:22:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 15:23:32 +00:00
|
|
|
return AVERROR(EIO);
|
2006-03-27 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2011-01-25 22:03:28 +00:00
|
|
|
AVInputFormat ff_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
|
|
|
};
|