lxfdec: support version 1 files, too.
Fixes trac issue #656. Signed-off-by: Reimar Döffinger <Reimar.Doeffinger@gmx.de>
This commit is contained in:
parent
40393ac568
commit
1a06d6dd86
@ -20,11 +20,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "libavutil/intreadwrite.h"
|
#include "libavutil/intreadwrite.h"
|
||||||
|
#include "libavcodec/bytestream.h"
|
||||||
#include "avformat.h"
|
#include "avformat.h"
|
||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
#include "riff.h"
|
#include "riff.h"
|
||||||
|
|
||||||
#define LXF_PACKET_HEADER_SIZE 60
|
#define LXF_MAX_PACKET_HEADER_SIZE 256
|
||||||
#define LXF_HEADER_DATA_SIZE 120
|
#define LXF_HEADER_DATA_SIZE 120
|
||||||
#define LXF_IDENT "LEITCH\0"
|
#define LXF_IDENT "LEITCH\0"
|
||||||
#define LXF_IDENT_LENGTH 8
|
#define LXF_IDENT_LENGTH 8
|
||||||
@ -49,6 +50,7 @@ typedef struct {
|
|||||||
int channels; ///< number of audio channels. zero means no audio
|
int channels; ///< number of audio channels. zero means no audio
|
||||||
uint8_t temp[LXF_MAX_AUDIO_PACKET]; ///< temp buffer for de-planarizing the audio data
|
uint8_t temp[LXF_MAX_AUDIO_PACKET]; ///< temp buffer for de-planarizing the audio data
|
||||||
int frame_number; ///< current video frame
|
int frame_number; ///< current video frame
|
||||||
|
uint32_t video_format, packet_type, extended_size;
|
||||||
} LXFDemuxContext;
|
} LXFDemuxContext;
|
||||||
|
|
||||||
static int lxf_probe(AVProbeData *p)
|
static int lxf_probe(AVProbeData *p)
|
||||||
@ -65,12 +67,12 @@ static int lxf_probe(AVProbeData *p)
|
|||||||
* @param[in] header the packet header to check
|
* @param[in] header the packet header to check
|
||||||
* @return zero if the checksum is OK, non-zero otherwise
|
* @return zero if the checksum is OK, non-zero otherwise
|
||||||
*/
|
*/
|
||||||
static int check_checksum(const uint8_t *header)
|
static int check_checksum(const uint8_t *header, int size)
|
||||||
{
|
{
|
||||||
int x;
|
int x;
|
||||||
uint32_t sum = 0;
|
uint32_t sum = 0;
|
||||||
|
|
||||||
for (x = 0; x < LXF_PACKET_HEADER_SIZE; x += 4)
|
for (x = 0; x < size; x += 4)
|
||||||
sum += AV_RL32(&header[x]);
|
sum += AV_RL32(&header[x]);
|
||||||
|
|
||||||
return sum;
|
return sum;
|
||||||
@ -106,40 +108,60 @@ static int sync(AVFormatContext *s, uint8_t *header)
|
|||||||
/**
|
/**
|
||||||
* Read and checksum the next packet header
|
* Read and checksum the next packet header
|
||||||
*
|
*
|
||||||
* @param[out] header the read packet header
|
|
||||||
* @param[out] format context dependent format information
|
|
||||||
* @return the size of the payload following the header or < 0 on failure
|
* @return the size of the payload following the header or < 0 on failure
|
||||||
*/
|
*/
|
||||||
static int get_packet_header(AVFormatContext *s, uint8_t *header, uint32_t *format)
|
static int get_packet_header(AVFormatContext *s)
|
||||||
{
|
{
|
||||||
|
LXFDemuxContext *lxf = s->priv_data;
|
||||||
AVIOContext *pb = s->pb;
|
AVIOContext *pb = s->pb;
|
||||||
int track_size, samples, ret;
|
int track_size, samples, ret;
|
||||||
|
uint32_t version, audio_format, header_size, channels, tmp;
|
||||||
AVStream *st;
|
AVStream *st;
|
||||||
|
uint8_t header[LXF_MAX_PACKET_HEADER_SIZE];
|
||||||
|
const uint8_t *p;
|
||||||
|
|
||||||
//find and read the ident
|
//find and read the ident
|
||||||
if ((ret = sync(s, header)) < 0)
|
if ((ret = sync(s, header)) < 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
|
ret = avio_read(pb, header + LXF_IDENT_LENGTH, 8);
|
||||||
|
if (ret != 8)
|
||||||
|
return ret < 0 ? ret : AVERROR_EOF;
|
||||||
|
|
||||||
|
p = header + LXF_IDENT_LENGTH;
|
||||||
|
version = bytestream_get_le32(&p);
|
||||||
|
header_size = bytestream_get_le32(&p);
|
||||||
|
if (version > 1)
|
||||||
|
av_log_ask_for_sample(s, "Unknown format version %i\n", version);
|
||||||
|
if (header_size < (version ? 72 : 60) ||
|
||||||
|
header_size > LXF_MAX_PACKET_HEADER_SIZE ||
|
||||||
|
(header_size & 3)) {
|
||||||
|
av_log(s, AV_LOG_ERROR, "Invalid header size 0x%x\n", header_size);
|
||||||
|
return AVERROR_INVALIDDATA;
|
||||||
|
}
|
||||||
|
|
||||||
//read the rest of the packet header
|
//read the rest of the packet header
|
||||||
if ((ret = avio_read(pb, header + LXF_IDENT_LENGTH,
|
if ((ret = avio_read(pb, header + (p - header),
|
||||||
LXF_PACKET_HEADER_SIZE - LXF_IDENT_LENGTH)) !=
|
header_size - (p - header))) !=
|
||||||
LXF_PACKET_HEADER_SIZE - LXF_IDENT_LENGTH) {
|
header_size - (p - header)) {
|
||||||
return ret < 0 ? ret : AVERROR_EOF;
|
return ret < 0 ? ret : AVERROR_EOF;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (check_checksum(header))
|
if (check_checksum(header, header_size))
|
||||||
av_log(s, AV_LOG_ERROR, "checksum error\n");
|
av_log(s, AV_LOG_ERROR, "checksum error\n");
|
||||||
|
|
||||||
*format = AV_RL32(&header[32]);
|
lxf->packet_type = bytestream_get_le32(&p);
|
||||||
ret = AV_RL32(&header[36]);
|
p += version ? 20 : 12;
|
||||||
|
|
||||||
//type
|
lxf->extended_size = 0;
|
||||||
switch (AV_RL32(&header[16])) {
|
switch (lxf->packet_type) {
|
||||||
case 0:
|
case 0:
|
||||||
//video
|
//video
|
||||||
|
lxf->video_format = bytestream_get_le32(&p);
|
||||||
|
ret = bytestream_get_le32(&p);
|
||||||
//skip VBI data and metadata
|
//skip VBI data and metadata
|
||||||
avio_skip(pb, (int64_t)(uint32_t)AV_RL32(&header[44]) +
|
avio_skip(pb, (int64_t)(uint32_t)AV_RL32(p + 4) +
|
||||||
(int64_t)(uint32_t)AV_RL32(&header[52]));
|
(int64_t)(uint32_t)AV_RL32(p + 12));
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
//audio
|
//audio
|
||||||
@ -148,12 +170,16 @@ static int get_packet_header(AVFormatContext *s, uint8_t *header, uint32_t *form
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (version == 0) p += 8;
|
||||||
|
audio_format = bytestream_get_le32(&p);
|
||||||
|
channels = bytestream_get_le32(&p);
|
||||||
|
track_size = bytestream_get_le32(&p);
|
||||||
|
|
||||||
//set codec based on specified audio bitdepth
|
//set codec based on specified audio bitdepth
|
||||||
//we only support tightly packed 16-, 20-, 24- and 32-bit PCM at the moment
|
//we only support tightly packed 16-, 20-, 24- and 32-bit PCM at the moment
|
||||||
*format = AV_RL32(&header[40]);
|
st->codec->bits_per_coded_sample = (audio_format >> 6) & 0x3F;
|
||||||
st->codec->bits_per_coded_sample = (*format >> 6) & 0x3F;
|
|
||||||
|
|
||||||
if (st->codec->bits_per_coded_sample != (*format & 0x3F)) {
|
if (st->codec->bits_per_coded_sample != (audio_format & 0x3F)) {
|
||||||
av_log(s, AV_LOG_WARNING, "only tightly packed PCM currently supported\n");
|
av_log(s, AV_LOG_WARNING, "only tightly packed PCM currently supported\n");
|
||||||
return AVERROR_PATCHWELCOME;
|
return AVERROR_PATCHWELCOME;
|
||||||
}
|
}
|
||||||
@ -169,7 +195,6 @@ static int get_packet_header(AVFormatContext *s, uint8_t *header, uint32_t *form
|
|||||||
return AVERROR_PATCHWELCOME;
|
return AVERROR_PATCHWELCOME;
|
||||||
}
|
}
|
||||||
|
|
||||||
track_size = AV_RL32(&header[48]);
|
|
||||||
samples = track_size * 8 / st->codec->bits_per_coded_sample;
|
samples = track_size * 8 / st->codec->bits_per_coded_sample;
|
||||||
|
|
||||||
//use audio packet size to determine video standard
|
//use audio packet size to determine video standard
|
||||||
@ -186,10 +211,14 @@ static int get_packet_header(AVFormatContext *s, uint8_t *header, uint32_t *form
|
|||||||
}
|
}
|
||||||
|
|
||||||
//TODO: warning if track mask != (1 << channels) - 1?
|
//TODO: warning if track mask != (1 << channels) - 1?
|
||||||
ret = av_popcount(AV_RL32(&header[44])) * track_size;
|
ret = av_popcount(channels) * track_size;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
tmp = bytestream_get_le32(&p);
|
||||||
|
ret = bytestream_get_le32(&p);
|
||||||
|
if (tmp == 1)
|
||||||
|
lxf->extended_size = bytestream_get_le32(&p);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,13 +229,13 @@ static int lxf_read_header(AVFormatContext *s)
|
|||||||
{
|
{
|
||||||
LXFDemuxContext *lxf = s->priv_data;
|
LXFDemuxContext *lxf = s->priv_data;
|
||||||
AVIOContext *pb = s->pb;
|
AVIOContext *pb = s->pb;
|
||||||
uint8_t header[LXF_PACKET_HEADER_SIZE], header_data[LXF_HEADER_DATA_SIZE];
|
uint8_t header_data[LXF_HEADER_DATA_SIZE];
|
||||||
int ret;
|
int ret;
|
||||||
AVStream *st;
|
AVStream *st;
|
||||||
uint32_t format, video_params, disk_params;
|
uint32_t video_params, disk_params;
|
||||||
uint16_t record_date, expiration_date;
|
uint16_t record_date, expiration_date;
|
||||||
|
|
||||||
if ((ret = get_packet_header(s, header, &format)) < 0)
|
if ((ret = get_packet_header(s)) < 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
if (ret != LXF_HEADER_DATA_SIZE) {
|
if (ret != LXF_HEADER_DATA_SIZE) {
|
||||||
@ -254,10 +283,7 @@ static int lxf_read_header(AVFormatContext *s)
|
|||||||
avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
|
avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (format == 1) {
|
avio_skip(s->pb, lxf->extended_size);
|
||||||
//skip extended field data
|
|
||||||
avio_skip(s->pb, (uint32_t)AV_RL32(&header[40]));
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -283,15 +309,15 @@ static int lxf_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|||||||
{
|
{
|
||||||
LXFDemuxContext *lxf = s->priv_data;
|
LXFDemuxContext *lxf = s->priv_data;
|
||||||
AVIOContext *pb = s->pb;
|
AVIOContext *pb = s->pb;
|
||||||
uint8_t header[LXF_PACKET_HEADER_SIZE], *buf;
|
uint8_t *buf;
|
||||||
AVStream *ast = NULL;
|
AVStream *ast = NULL;
|
||||||
uint32_t stream, format;
|
uint32_t stream;
|
||||||
int ret, ret2;
|
int ret, ret2;
|
||||||
|
|
||||||
if ((ret = get_packet_header(s, header, &format)) < 0)
|
if ((ret = get_packet_header(s)) < 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
stream = AV_RL32(&header[16]);
|
stream = lxf->packet_type;
|
||||||
|
|
||||||
if (stream > 1) {
|
if (stream > 1) {
|
||||||
av_log(s, AV_LOG_WARNING, "got packet with illegal stream index %u\n", stream);
|
av_log(s, AV_LOG_WARNING, "got packet with illegal stream index %u\n", stream);
|
||||||
@ -328,7 +354,7 @@ static int lxf_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|||||||
deplanarize(lxf, ast, pkt->data, ret);
|
deplanarize(lxf, ast, pkt->data, ret);
|
||||||
} else {
|
} else {
|
||||||
//picture type (0 = closed I, 1 = open I, 2 = P, 3 = B)
|
//picture type (0 = closed I, 1 = open I, 2 = P, 3 = B)
|
||||||
if (((format >> 22) & 0x3) < 2)
|
if (((lxf->video_format >> 22) & 0x3) < 2)
|
||||||
pkt->flags |= AV_PKT_FLAG_KEY;
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
||||||
|
|
||||||
pkt->dts = lxf->frame_number++;
|
pkt->dts = lxf->frame_number++;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user