2011-03-26 16:25:10 +01:00
|
|
|
/*
|
|
|
|
* PMP demuxer.
|
|
|
|
* Copyright (c) 2011 Reimar Döffinger
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "avformat.h"
|
2011-12-03 00:45:46 +01:00
|
|
|
#include "internal.h"
|
2011-03-26 16:25:10 +01:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int cur_stream;
|
|
|
|
int num_streams;
|
|
|
|
int audio_packets;
|
|
|
|
int current_packet;
|
|
|
|
uint32_t *packet_sizes;
|
|
|
|
int packet_sizes_alloc;
|
|
|
|
} PMPContext;
|
|
|
|
|
|
|
|
static int pmp_probe(AVProbeData *p) {
|
|
|
|
if (AV_RN32(p->buf) == AV_RN32("pmpm") &&
|
|
|
|
AV_RL32(p->buf + 4) == 1)
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-12 13:20:36 +01:00
|
|
|
static int pmp_header(AVFormatContext *s)
|
2011-03-26 16:25:10 +01:00
|
|
|
{
|
2011-03-26 16:25:10 +01:00
|
|
|
PMPContext *pmp = s->priv_data;
|
|
|
|
AVIOContext *pb = s->pb;
|
|
|
|
int tb_num, tb_den;
|
2013-02-23 21:50:22 +01:00
|
|
|
uint32_t index_cnt;
|
2012-08-05 11:11:04 +02:00
|
|
|
int audio_codec_id = AV_CODEC_ID_NONE;
|
2011-03-26 16:25:10 +01:00
|
|
|
int srate, channels;
|
2013-02-24 23:37:47 +01:00
|
|
|
unsigned i;
|
2011-03-26 16:25:10 +01:00
|
|
|
uint64_t pos;
|
2013-02-23 22:05:30 +01:00
|
|
|
int64_t fsize = avio_size(pb);
|
|
|
|
|
2011-11-05 13:11:18 +01:00
|
|
|
AVStream *vst = avformat_new_stream(s, NULL);
|
2011-03-26 16:25:10 +01:00
|
|
|
if (!vst)
|
|
|
|
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 20:42:52 +02:00
|
|
|
vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
2011-03-26 16:25:10 +01:00
|
|
|
avio_skip(pb, 8);
|
|
|
|
switch (avio_rl32(pb)) {
|
|
|
|
case 0:
|
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 20:42:52 +02:00
|
|
|
vst->codecpar->codec_id = AV_CODEC_ID_MPEG4;
|
2011-03-26 16:25:10 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
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 20:42:52 +02:00
|
|
|
vst->codecpar->codec_id = AV_CODEC_ID_H264;
|
2011-03-26 16:25:10 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(s, AV_LOG_ERROR, "Unsupported video format\n");
|
|
|
|
break;
|
|
|
|
}
|
2011-03-26 16:25:10 +01:00
|
|
|
index_cnt = avio_rl32(pb);
|
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 20:42:52 +02:00
|
|
|
vst->codecpar->width = avio_rl32(pb);
|
|
|
|
vst->codecpar->height = avio_rl32(pb);
|
2011-03-26 16:25:10 +01:00
|
|
|
|
|
|
|
tb_num = avio_rl32(pb);
|
|
|
|
tb_den = avio_rl32(pb);
|
2011-12-03 00:45:46 +01:00
|
|
|
avpriv_set_pts_info(vst, 32, tb_num, tb_den);
|
2011-03-26 16:25:10 +01:00
|
|
|
vst->nb_frames = index_cnt;
|
|
|
|
vst->duration = index_cnt;
|
|
|
|
|
|
|
|
switch (avio_rl32(pb)) {
|
|
|
|
case 0:
|
2012-08-05 11:11:04 +02:00
|
|
|
audio_codec_id = AV_CODEC_ID_MP3;
|
2011-03-26 16:25:10 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
av_log(s, AV_LOG_ERROR, "AAC not yet correctly supported\n");
|
2012-08-05 11:11:04 +02:00
|
|
|
audio_codec_id = AV_CODEC_ID_AAC;
|
2011-03-26 16:25:10 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(s, AV_LOG_ERROR, "Unsupported audio format\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pmp->num_streams = avio_rl16(pb) + 1;
|
|
|
|
avio_skip(pb, 10);
|
|
|
|
srate = avio_rl32(pb);
|
|
|
|
channels = avio_rl32(pb) + 1;
|
2013-02-24 22:53:58 +01:00
|
|
|
pos = avio_tell(pb) + 4LL*index_cnt;
|
2011-03-26 16:25:10 +01:00
|
|
|
for (i = 0; i < index_cnt; i++) {
|
2013-02-23 21:50:22 +01:00
|
|
|
uint32_t size = avio_rl32(pb);
|
2011-03-26 16:25:10 +01:00
|
|
|
int flags = size & 1 ? AVINDEX_KEYFRAME : 0;
|
2014-08-07 22:12:41 +02:00
|
|
|
if (avio_feof(pb)) {
|
2013-02-23 19:44:46 +01:00
|
|
|
av_log(s, AV_LOG_FATAL, "Encountered EOF while reading index.\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2011-03-26 16:25:10 +01:00
|
|
|
size >>= 1;
|
2013-02-23 22:05:30 +01:00
|
|
|
if (size < 9 + 4*pmp->num_streams) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Packet too small\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2011-03-26 16:25:10 +01:00
|
|
|
av_add_index_entry(vst, pos, i, size, 0, flags);
|
|
|
|
pos += size;
|
2013-02-23 22:05:30 +01:00
|
|
|
if (fsize > 0 && i == 0 && pos > fsize) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "File ends before first packet\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
2011-03-26 16:25:10 +01:00
|
|
|
}
|
2013-02-23 21:47:15 +01:00
|
|
|
for (i = 1; i < pmp->num_streams; i++) {
|
|
|
|
AVStream *ast = avformat_new_stream(s, NULL);
|
|
|
|
if (!ast)
|
|
|
|
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 20:42:52 +02:00
|
|
|
ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
ast->codecpar->codec_id = audio_codec_id;
|
|
|
|
ast->codecpar->channels = channels;
|
|
|
|
ast->codecpar->sample_rate = srate;
|
2013-02-23 21:47:15 +01:00
|
|
|
avpriv_set_pts_info(ast, 32, 1, srate);
|
|
|
|
}
|
2011-03-26 16:25:10 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-26 16:25:10 +01:00
|
|
|
static int pmp_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
2011-03-26 16:25:10 +01:00
|
|
|
PMPContext *pmp = s->priv_data;
|
|
|
|
AVIOContext *pb = s->pb;
|
|
|
|
int ret = 0;
|
|
|
|
int i;
|
|
|
|
|
2014-08-07 22:12:41 +02:00
|
|
|
if (avio_feof(pb))
|
2011-03-26 16:25:10 +01:00
|
|
|
return AVERROR_EOF;
|
|
|
|
if (pmp->cur_stream == 0) {
|
|
|
|
int num_packets;
|
|
|
|
pmp->audio_packets = avio_r8(pb);
|
2014-01-04 01:35:17 +01:00
|
|
|
|
2012-02-28 07:19:37 +01:00
|
|
|
if (!pmp->audio_packets) {
|
2013-11-28 10:54:35 +01:00
|
|
|
av_log(s, AV_LOG_ERROR, "No audio packets.\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
2012-02-28 07:19:37 +01:00
|
|
|
}
|
2013-11-28 10:54:35 +01:00
|
|
|
|
2011-03-26 16:25:10 +01:00
|
|
|
num_packets = (pmp->num_streams - 1) * pmp->audio_packets + 1;
|
|
|
|
avio_skip(pb, 8);
|
|
|
|
pmp->current_packet = 0;
|
|
|
|
av_fast_malloc(&pmp->packet_sizes,
|
|
|
|
&pmp->packet_sizes_alloc,
|
|
|
|
num_packets * sizeof(*pmp->packet_sizes));
|
2011-03-26 16:25:10 +01:00
|
|
|
if (!pmp->packet_sizes_alloc) {
|
|
|
|
av_log(s, AV_LOG_ERROR, "Cannot (re)allocate packet buffer\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2011-03-26 16:25:10 +01:00
|
|
|
for (i = 0; i < num_packets; i++)
|
|
|
|
pmp->packet_sizes[i] = avio_rl32(pb);
|
|
|
|
}
|
|
|
|
ret = av_get_packet(pb, pkt, pmp->packet_sizes[pmp->current_packet]);
|
|
|
|
if (ret >= 0) {
|
|
|
|
ret = 0;
|
|
|
|
pkt->stream_index = pmp->cur_stream;
|
|
|
|
}
|
|
|
|
if (pmp->current_packet % pmp->audio_packets == 0)
|
|
|
|
pmp->cur_stream = (pmp->cur_stream + 1) % pmp->num_streams;
|
|
|
|
pmp->current_packet++;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-12-02 00:51:11 +01:00
|
|
|
static int pmp_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
|
2011-03-26 16:25:10 +01:00
|
|
|
{
|
2011-03-26 16:25:10 +01:00
|
|
|
PMPContext *pmp = s->priv_data;
|
|
|
|
pmp->cur_stream = 0;
|
2012-08-21 01:02:13 +02:00
|
|
|
// fall back on default seek now
|
2011-03-26 16:25:10 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pmp_close(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
PMPContext *pmp = s->priv_data;
|
|
|
|
av_freep(&pmp->packet_sizes);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVInputFormat ff_pmp_demuxer = {
|
|
|
|
.name = "pmp",
|
2012-07-24 23:51:41 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Playstation Portable PMP"),
|
2011-03-26 16:25:10 +01:00
|
|
|
.priv_data_size = sizeof(PMPContext),
|
|
|
|
.read_probe = pmp_probe,
|
|
|
|
.read_header = pmp_header,
|
|
|
|
.read_packet = pmp_packet,
|
|
|
|
.read_seek = pmp_seek,
|
|
|
|
.read_close = pmp_close,
|
|
|
|
};
|