2006-03-28 01:22:50 +03:00
|
|
|
/*
|
|
|
|
* NuppelVideo demuxer.
|
2009-01-19 17:46:40 +02:00
|
|
|
* Copyright (c) 2006 Reimar Doeffinger
|
2006-03-28 01:22:50 +03:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* This file is part of Libav.
|
2006-10-07 18:30:46 +03:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2006-03-28 01:22:50 +03: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.
|
2006-03-28 01:22:50 +03:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2006-03-28 01:22:50 +03: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
|
2011-03-18 19:35:10 +02:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2006-03-28 01:22:50 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2009-01-12 00:19:48 +02:00
|
|
|
|
|
|
|
#include "libavutil/intreadwrite.h"
|
2011-06-04 14:42:16 +03:00
|
|
|
#include "libavutil/intfloat_readwrite.h"
|
2006-03-28 01:22:50 +03:00
|
|
|
#include "avformat.h"
|
2006-07-12 03:09:34 +03:00
|
|
|
#include "riff.h"
|
2006-03-28 01:22:50 +03:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int v_id;
|
|
|
|
int a_id;
|
2007-09-09 10:25:52 +03:00
|
|
|
int rtjpg_video;
|
2006-03-28 01:22:50 +03:00
|
|
|
} NUVContext;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
NUV_VIDEO = 'V',
|
|
|
|
NUV_EXTRADATA = 'D',
|
|
|
|
NUV_AUDIO = 'A',
|
|
|
|
NUV_SEEKP = 'R',
|
|
|
|
NUV_MYTHEXT = 'X'
|
2008-12-11 21:19:23 +02:00
|
|
|
} nuv_frametype;
|
2006-03-28 01:22:50 +03: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)
|
|
|
|
|
|
|
|
/**
|
2011-06-23 23:41:54 +03:00
|
|
|
* @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
|
2006-03-28 01:22:50 +03:00
|
|
|
*/
|
2011-02-20 12:04:12 +02:00
|
|
|
static int get_codec_data(AVIOContext *pb, AVStream *vst,
|
2006-03-28 01:22:50 +03:00
|
|
|
AVStream *ast, int myth) {
|
2008-12-11 21:19:23 +02:00
|
|
|
nuv_frametype frametype;
|
2006-03-28 01:22:50 +03:00
|
|
|
if (!vst && !myth)
|
|
|
|
return 1; // no codec data needed
|
2011-03-07 22:50:25 +02:00
|
|
|
while (!pb->eof_reached) {
|
2006-03-28 01:22:50 +03:00
|
|
|
int size, subtype;
|
2011-02-21 17:43:01 +02:00
|
|
|
frametype = avio_r8(pb);
|
2006-03-28 01:22:50 +03:00
|
|
|
switch (frametype) {
|
|
|
|
case NUV_EXTRADATA:
|
2011-02-21 17:43:01 +02:00
|
|
|
subtype = avio_r8(pb);
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, 6);
|
2011-02-21 17:43:01 +02:00
|
|
|
size = PKTSIZE(avio_rl32(pb));
|
2006-04-20 13:38:15 +03:00
|
|
|
if (vst && subtype == 'R') {
|
2006-03-28 01:22:50 +03:00
|
|
|
vst->codec->extradata_size = size;
|
|
|
|
vst->codec->extradata = av_malloc(size);
|
2011-02-21 17:43:01 +02:00
|
|
|
avio_read(pb, vst->codec->extradata, size);
|
2006-03-28 01:22:50 +03:00
|
|
|
size = 0;
|
|
|
|
if (!myth)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NUV_MYTHEXT:
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, 7);
|
2011-02-21 17:43:01 +02:00
|
|
|
size = PKTSIZE(avio_rl32(pb));
|
2006-03-28 01:22:50 +03:00
|
|
|
if (size != 128 * 4)
|
|
|
|
break;
|
2011-02-21 17:43:01 +02:00
|
|
|
avio_rl32(pb); // version
|
2006-03-28 01:22:50 +03:00
|
|
|
if (vst) {
|
2011-02-21 17:43:01 +02:00
|
|
|
vst->codec->codec_tag = avio_rl32(pb);
|
2006-03-28 01:22:50 +03:00
|
|
|
vst->codec->codec_id =
|
2009-06-23 02:09:34 +03:00
|
|
|
ff_codec_get_id(ff_codec_bmp_tags, vst->codec->codec_tag);
|
2007-09-15 11:19:01 +03:00
|
|
|
if (vst->codec->codec_tag == MKTAG('R', 'J', 'P', 'G'))
|
|
|
|
vst->codec->codec_id = CODEC_ID_NUV;
|
2006-03-28 01:22:50 +03:00
|
|
|
} else
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, 4);
|
2006-03-28 01:22:50 +03:00
|
|
|
|
|
|
|
if (ast) {
|
2011-02-21 17:43:01 +02: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-28 01:22:50 +03:00
|
|
|
ast->codec->codec_id =
|
2009-06-23 02:09:34 +03:00
|
|
|
ff_wav_codec_get_id(ast->codec->codec_tag,
|
2008-09-08 17:24:59 +03:00
|
|
|
ast->codec->bits_per_coded_sample);
|
2007-05-27 11:06:45 +03:00
|
|
|
ast->need_parsing = AVSTREAM_PARSE_FULL;
|
2006-03-28 01:22:50 +03:00
|
|
|
} else
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, 4 * 4);
|
2006-03-28 01:22:50 +03:00
|
|
|
|
|
|
|
size -= 6 * 4;
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, size);
|
2006-03-28 01:22:50 +03:00
|
|
|
return 1;
|
|
|
|
case NUV_SEEKP:
|
|
|
|
size = 11;
|
|
|
|
break;
|
|
|
|
default:
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, 7);
|
2011-02-21 17:43:01 +02:00
|
|
|
size = PKTSIZE(avio_rl32(pb));
|
2006-03-28 01:22:50 +03:00
|
|
|
break;
|
|
|
|
}
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, size);
|
2006-03-28 01:22:50 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nuv_header(AVFormatContext *s, AVFormatParameters *ap) {
|
2007-04-08 23:24:16 +03:00
|
|
|
NUVContext *ctx = s->priv_data;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *pb = s->pb;
|
2009-04-08 00:16:11 +03:00
|
|
|
char id_string[12];
|
2006-03-28 01:22:50 +03: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 17:43:01 +02:00
|
|
|
avio_read(pb, id_string, 12);
|
2006-03-28 01:22:50 +03:00
|
|
|
is_mythtv = !memcmp(id_string, "MythTVVideo", 12);
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, 5); // version string
|
|
|
|
avio_skip(pb, 3); // padding
|
2011-02-21 17:43:01 +02: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-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, 3); // padding
|
2011-02-21 17:43:01 +02:00
|
|
|
aspect = av_int2dbl(avio_rl64(pb));
|
2008-07-25 07:10:32 +03:00
|
|
|
if (aspect > 0.9999 && aspect < 1.0001)
|
|
|
|
aspect = 4.0 / 3.0;
|
2011-02-21 17:43:01 +02:00
|
|
|
fps = av_int2dbl(avio_rl64(pb));
|
2006-03-28 01:22:50 +03:00
|
|
|
|
|
|
|
// number of packets per stream type, -1 means unknown, e.g. streaming
|
2011-02-21 17:43:01 +02:00
|
|
|
v_packs = avio_rl32(pb);
|
|
|
|
a_packs = avio_rl32(pb);
|
|
|
|
avio_rl32(pb); // text
|
2006-03-28 01:22:50 +03:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
avio_rl32(pb); // keyframe distance (?)
|
2006-03-28 01:22:50 +03:00
|
|
|
|
|
|
|
if (v_packs) {
|
|
|
|
ctx->v_id = stream_nr++;
|
|
|
|
vst = av_new_stream(s, ctx->v_id);
|
2008-05-29 18:25:45 +03:00
|
|
|
if (!vst)
|
|
|
|
return AVERROR(ENOMEM);
|
2010-03-31 02:30:55 +03:00
|
|
|
vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
|
2006-03-28 01:22:50 +03:00
|
|
|
vst->codec->codec_id = CODEC_ID_NUV;
|
|
|
|
vst->codec->width = width;
|
|
|
|
vst->codec->height = height;
|
2008-09-08 17:24:59 +03:00
|
|
|
vst->codec->bits_per_coded_sample = 10;
|
2008-08-24 02:43:20 +03:00
|
|
|
vst->sample_aspect_ratio = av_d2q(aspect * height / width, 10000);
|
2007-01-31 13:24:04 +02:00
|
|
|
vst->r_frame_rate = av_d2q(fps, 60000);
|
2006-03-28 01:22:50 +03: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 18:25:45 +03:00
|
|
|
if (!ast)
|
|
|
|
return AVERROR(ENOMEM);
|
2010-03-31 02:30:55 +03:00
|
|
|
ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
|
2006-03-28 01:22:50 +03: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 17:24:59 +03:00
|
|
|
ast->codec->bits_per_coded_sample = 16;
|
2006-03-28 01:22:50 +03: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 19:30:39 +03:00
|
|
|
ctx->rtjpg_video = vst && vst->codec->codec_id == CODEC_ID_NUV;
|
2006-03-28 01:22:50 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define HDRSIZE 12
|
|
|
|
|
|
|
|
static int nuv_packet(AVFormatContext *s, AVPacket *pkt) {
|
2007-04-08 23:24:16 +03:00
|
|
|
NUVContext *ctx = s->priv_data;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *pb = s->pb;
|
2006-03-28 01:22:50 +03:00
|
|
|
uint8_t hdr[HDRSIZE];
|
2008-12-11 21:19:23 +02:00
|
|
|
nuv_frametype frametype;
|
2006-03-28 01:22:50 +03:00
|
|
|
int ret, size;
|
2011-03-07 22:50:25 +02:00
|
|
|
while (!pb->eof_reached) {
|
2007-09-09 10:25:52 +03:00
|
|
|
int copyhdrsize = ctx->rtjpg_video ? HDRSIZE : 0;
|
2011-03-03 21:11:45 +02:00
|
|
|
uint64_t pos = avio_tell(pb);
|
2011-02-21 17:43:01 +02:00
|
|
|
ret = avio_read(pb, hdr, HDRSIZE);
|
2009-04-07 23:52:37 +03:00
|
|
|
if (ret < HDRSIZE)
|
|
|
|
return ret < 0 ? ret : AVERROR(EIO);
|
2006-03-28 01:22:50 +03:00
|
|
|
frametype = hdr[0];
|
2007-01-20 00:12:59 +02:00
|
|
|
size = PKTSIZE(AV_RL32(&hdr[8]));
|
2006-03-28 01:22:50 +03:00
|
|
|
switch (frametype) {
|
|
|
|
case NUV_EXTRADATA:
|
2007-09-09 10:25:52 +03:00
|
|
|
if (!ctx->rtjpg_video) {
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, size);
|
2007-09-09 10:25:52 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NUV_VIDEO:
|
2006-03-28 01:22:50 +03:00
|
|
|
if (ctx->v_id < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Video packet in file without video stream!\n");
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, size);
|
2006-03-28 01:22:50 +03:00
|
|
|
break;
|
|
|
|
}
|
2007-09-09 10:25:52 +03:00
|
|
|
ret = av_new_packet(pkt, copyhdrsize + size);
|
2006-03-28 01:22:50 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2009-03-09 14:25:13 +02: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 15:29:58 +03:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2009-03-09 13:35:59 +02:00
|
|
|
pkt->pos = pos;
|
2007-01-20 00:12:59 +02:00
|
|
|
pkt->pts = AV_RL32(&hdr[4]);
|
2006-03-28 01:22:50 +03:00
|
|
|
pkt->stream_index = ctx->v_id;
|
2007-09-09 10:25:52 +03:00
|
|
|
memcpy(pkt->data, hdr, copyhdrsize);
|
2011-02-21 17:43:01 +02:00
|
|
|
ret = avio_read(pb, pkt->data + copyhdrsize, size);
|
2009-04-09 17:16:22 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
av_free_packet(pkt);
|
|
|
|
return ret;
|
|
|
|
}
|
2009-04-08 23:20:42 +03:00
|
|
|
if (ret < size)
|
|
|
|
av_shrink_packet(pkt, copyhdrsize + ret);
|
2009-04-08 00:25:02 +03:00
|
|
|
return 0;
|
2006-03-28 01:22:50 +03:00
|
|
|
case NUV_AUDIO:
|
|
|
|
if (ctx->a_id < 0) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Audio packet in file without audio stream!\n");
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, size);
|
2006-03-28 01:22:50 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret = av_get_packet(pb, pkt, size);
|
2010-03-31 15:29:58 +03:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2009-03-09 13:35:59 +02:00
|
|
|
pkt->pos = pos;
|
2007-01-20 00:12:59 +02:00
|
|
|
pkt->pts = AV_RL32(&hdr[4]);
|
2006-03-28 01:22:50 +03:00
|
|
|
pkt->stream_index = ctx->a_id;
|
2009-04-08 00:25:02 +03:00
|
|
|
if (ret < 0) return ret;
|
|
|
|
return 0;
|
2006-03-28 01:22:50 +03:00
|
|
|
case NUV_SEEKP:
|
|
|
|
// contains no data, size value is invalid
|
|
|
|
break;
|
|
|
|
default:
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, size);
|
2006-03-28 01:22:50 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 18:23:32 +03:00
|
|
|
return AVERROR(EIO);
|
2006-03-28 01:22:50 +03:00
|
|
|
}
|
|
|
|
|
2011-01-26 00:03:28 +02:00
|
|
|
AVInputFormat ff_nuv_demuxer = {
|
2011-07-16 23:18:12 +03:00
|
|
|
.name = "nuv",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("NuppelVideo format"),
|
|
|
|
.priv_data_size = sizeof(NUVContext),
|
|
|
|
.read_probe = nuv_probe,
|
|
|
|
.read_header = nuv_header,
|
|
|
|
.read_packet = nuv_packet,
|
2009-03-15 00:07:15 +02:00
|
|
|
.flags = AVFMT_GENERIC_INDEX,
|
2006-03-28 01:22:50 +03:00
|
|
|
};
|