FFmpeg
Loading...
Searching...
No Matches
pmpdec.c
Go to the documentation of this file.
1/*
2 * PMP demuxer.
3 * Copyright (c) 2011 Reimar Döffinger
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
23#include "libavutil/mem.h"
24#include "avformat.h"
25#include "demux.h"
26#include "internal.h"
27
36
37static int pmp_probe(const AVProbeData *p) {
38 if (AV_RN32(p->buf) == AV_RN32("pmpm") &&
39 AV_RL32(p->buf + 4) == 1)
40 return AVPROBE_SCORE_MAX;
41 return 0;
42}
43
45{
46 PMPContext *pmp = s->priv_data;
47 AVIOContext *pb = s->pb;
48 int tb_num, tb_den;
49 uint32_t index_cnt;
50 int audio_codec_id = AV_CODEC_ID_NONE;
51 int srate, channels;
52 unsigned i;
53 uint64_t pos;
55
57 if (!vst)
58 return AVERROR(ENOMEM);
60 avio_skip(pb, 8);
61 switch (avio_rl32(pb)) {
62 case 0:
64 break;
65 case 1:
67 break;
68 default:
69 av_log(s, AV_LOG_ERROR, "Unsupported video format\n");
70 break;
71 }
72 index_cnt = avio_rl32(pb);
73 vst->codecpar->width = avio_rl32(pb);
74 vst->codecpar->height = avio_rl32(pb);
75
76 tb_num = avio_rl32(pb);
77 tb_den = avio_rl32(pb);
78 avpriv_set_pts_info(vst, 32, tb_num, tb_den);
79 vst->nb_frames = index_cnt;
80 vst->duration = index_cnt;
81
82 switch (avio_rl32(pb)) {
83 case 0:
84 audio_codec_id = AV_CODEC_ID_MP3;
85 break;
86 case 1:
87 av_log(s, AV_LOG_ERROR, "AAC not yet correctly supported\n");
88 audio_codec_id = AV_CODEC_ID_AAC;
89 break;
90 default:
91 av_log(s, AV_LOG_ERROR, "Unsupported audio format\n");
92 break;
93 }
94 pmp->num_streams = avio_rl16(pb) + 1;
95 avio_skip(pb, 10);
96 srate = avio_rl32(pb);
97 channels = avio_rl32(pb) + 1;
98 pos = avio_tell(pb) + 4LL*index_cnt;
99 for (i = 0; i < index_cnt; i++) {
100 uint32_t size = avio_rl32(pb);
101 int flags = size & 1 ? AVINDEX_KEYFRAME : 0;
102 if (avio_feof(pb)) {
103 av_log(s, AV_LOG_FATAL, "Encountered EOF while reading index.\n");
104 return AVERROR_INVALIDDATA;
105 }
106 size >>= 1;
107 if (size < 9 + 4*pmp->num_streams) {
108 av_log(s, AV_LOG_ERROR, "Packet too small\n");
109 return AVERROR_INVALIDDATA;
110 }
111 av_add_index_entry(vst, pos, i, size, 0, flags);
112 pos += size;
113 if (fsize > 0 && i == 0 && pos > fsize) {
114 av_log(s, AV_LOG_ERROR, "File ends before first packet\n");
115 return AVERROR_INVALIDDATA;
116 }
117 }
118 for (i = 1; i < pmp->num_streams; i++) {
120 if (!ast)
121 return AVERROR(ENOMEM);
123 ast->codecpar->codec_id = audio_codec_id;
125 ast->codecpar->sample_rate = srate;
126 avpriv_set_pts_info(ast, 32, 1, srate);
127 }
128 return 0;
129}
130
132{
133 PMPContext *pmp = s->priv_data;
134 AVIOContext *pb = s->pb;
135 int ret = 0;
136 int i;
137
138 if (avio_feof(pb))
139 return AVERROR_EOF;
140 if (pmp->cur_stream == 0) {
141 int num_packets;
142 pmp->audio_packets = avio_r8(pb);
143
144 if (!pmp->audio_packets) {
145 av_log(s, AV_LOG_ERROR, "No audio packets.\n");
146 return AVERROR_INVALIDDATA;
147 }
148
149 num_packets = (pmp->num_streams - 1) * pmp->audio_packets + 1;
150 avio_skip(pb, 8);
151 pmp->current_packet = 0;
153 &pmp->packet_sizes_alloc,
154 num_packets * sizeof(*pmp->packet_sizes));
155 if (!pmp->packet_sizes_alloc) {
156 av_log(s, AV_LOG_ERROR, "Cannot (re)allocate packet buffer\n");
157 return AVERROR(ENOMEM);
158 }
159 for (i = 0; i < num_packets; i++)
160 pmp->packet_sizes[i] = avio_rl32(pb);
161 }
162 ret = av_get_packet(pb, pkt, pmp->packet_sizes[pmp->current_packet]);
163 if (ret >= 0) {
164 ret = 0;
165 pkt->stream_index = pmp->cur_stream;
166 }
167 if (pmp->current_packet % pmp->audio_packets == 0)
168 pmp->cur_stream = (pmp->cur_stream + 1) % pmp->num_streams;
169 pmp->current_packet++;
170 return ret;
171}
172
173static int pmp_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
174{
175 PMPContext *pmp = s->priv_data;
176 pmp->cur_stream = 0;
177 // fall back on default seek now
178 return -1;
179}
180
182{
183 PMPContext *pmp = s->priv_data;
184 av_freep(&pmp->packet_sizes);
185 return 0;
186}
187
189 .p.name = "pmp",
190 .p.long_name = NULL_IF_CONFIG_SMALL("Playstation Portable PMP"),
191 .priv_data_size = sizeof(PMPContext),
197};
const FFInputFormat ff_pmp_demuxer
Definition pmpdec.c:188
channels
Definition aptx.h:31
static int64_t fsize(FILE *f)
Definition audiomatch.c:29
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVINDEX_KEYFRAME
Definition avformat.h:628
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_H264
Definition codec_id.h:77
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_AAC
Definition codec_id.h:455
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition codec_id.h:454
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition seek.c:122
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition log.h:204
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition mem.c:555
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define AV_RN32(p)
#define AV_RL32(p)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
Memory handling functions.
static int pmp_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
Definition pmpdec.c:173
static int pmp_probe(const AVProbeData *p)
Definition pmpdec.c:37
static int pmp_close(AVFormatContext *s)
Definition pmpdec.c:181
static int pmp_packet(AVFormatContext *s, AVPacket *pkt)
Definition pmpdec.c:131
static int pmp_header(AVFormatContext *s)
Definition pmpdec.c:44
unsigned int pos
Definition spdifenc.c:431
int nb_channels
Number of channels in this layout.
int height
The height of the video frame in pixels.
Definition codec_par.h:150
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t nb_frames
number of frames in this stream if known or 0
Definition avformat.h:827
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
int num_streams
Definition pmpdec.c:30
uint32_t * packet_sizes
Definition pmpdec.c:33
int audio_packets
Definition pmpdec.c:31
int packet_sizes_alloc
Definition pmpdec.c:34
int current_packet
Definition pmpdec.c:32
int cur_stream
Definition pmpdec.c:29
#define av_freep(p)
#define av_log(a,...)
int size