FFmpeg
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 
22 #include "libavutil/intreadwrite.h"
23 #include "avformat.h"
24 #include "demux.h"
25 #include "internal.h"
26 
27 typedef struct {
32  uint32_t *packet_sizes;
34 } PMPContext;
35 
36 static int pmp_probe(const AVProbeData *p) {
37  if (AV_RN32(p->buf) == AV_RN32("pmpm") &&
38  AV_RL32(p->buf + 4) == 1)
39  return AVPROBE_SCORE_MAX;
40  return 0;
41 }
42 
44 {
45  PMPContext *pmp = s->priv_data;
46  AVIOContext *pb = s->pb;
47  int tb_num, tb_den;
48  uint32_t index_cnt;
49  int audio_codec_id = AV_CODEC_ID_NONE;
50  int srate, channels;
51  unsigned i;
52  uint64_t pos;
53  int64_t fsize = avio_size(pb);
54 
56  if (!vst)
57  return AVERROR(ENOMEM);
59  avio_skip(pb, 8);
60  switch (avio_rl32(pb)) {
61  case 0:
63  break;
64  case 1:
66  break;
67  default:
68  av_log(s, AV_LOG_ERROR, "Unsupported video format\n");
69  break;
70  }
71  index_cnt = avio_rl32(pb);
72  vst->codecpar->width = avio_rl32(pb);
73  vst->codecpar->height = avio_rl32(pb);
74 
75  tb_num = avio_rl32(pb);
76  tb_den = avio_rl32(pb);
77  avpriv_set_pts_info(vst, 32, tb_num, tb_den);
78  vst->nb_frames = index_cnt;
79  vst->duration = index_cnt;
80 
81  switch (avio_rl32(pb)) {
82  case 0:
83  audio_codec_id = AV_CODEC_ID_MP3;
84  break;
85  case 1:
86  av_log(s, AV_LOG_ERROR, "AAC not yet correctly supported\n");
87  audio_codec_id = AV_CODEC_ID_AAC;
88  break;
89  default:
90  av_log(s, AV_LOG_ERROR, "Unsupported audio format\n");
91  break;
92  }
93  pmp->num_streams = avio_rl16(pb) + 1;
94  avio_skip(pb, 10);
95  srate = avio_rl32(pb);
96  channels = avio_rl32(pb) + 1;
97  pos = avio_tell(pb) + 4LL*index_cnt;
98  for (i = 0; i < index_cnt; i++) {
99  uint32_t size = avio_rl32(pb);
100  int flags = size & 1 ? AVINDEX_KEYFRAME : 0;
101  if (avio_feof(pb)) {
102  av_log(s, AV_LOG_FATAL, "Encountered EOF while reading index.\n");
103  return AVERROR_INVALIDDATA;
104  }
105  size >>= 1;
106  if (size < 9 + 4*pmp->num_streams) {
107  av_log(s, AV_LOG_ERROR, "Packet too small\n");
108  return AVERROR_INVALIDDATA;
109  }
110  av_add_index_entry(vst, pos, i, size, 0, flags);
111  pos += size;
112  if (fsize > 0 && i == 0 && pos > fsize) {
113  av_log(s, AV_LOG_ERROR, "File ends before first packet\n");
114  return AVERROR_INVALIDDATA;
115  }
116  }
117  for (i = 1; i < pmp->num_streams; i++) {
119  if (!ast)
120  return AVERROR(ENOMEM);
122  ast->codecpar->codec_id = audio_codec_id;
124  ast->codecpar->sample_rate = srate;
125  avpriv_set_pts_info(ast, 32, 1, srate);
126  }
127  return 0;
128 }
129 
131 {
132  PMPContext *pmp = s->priv_data;
133  AVIOContext *pb = s->pb;
134  int ret = 0;
135  int i;
136 
137  if (avio_feof(pb))
138  return AVERROR_EOF;
139  if (pmp->cur_stream == 0) {
140  int num_packets;
141  pmp->audio_packets = avio_r8(pb);
142 
143  if (!pmp->audio_packets) {
144  av_log(s, AV_LOG_ERROR, "No audio packets.\n");
145  return AVERROR_INVALIDDATA;
146  }
147 
148  num_packets = (pmp->num_streams - 1) * pmp->audio_packets + 1;
149  avio_skip(pb, 8);
150  pmp->current_packet = 0;
152  &pmp->packet_sizes_alloc,
153  num_packets * sizeof(*pmp->packet_sizes));
154  if (!pmp->packet_sizes_alloc) {
155  av_log(s, AV_LOG_ERROR, "Cannot (re)allocate packet buffer\n");
156  return AVERROR(ENOMEM);
157  }
158  for (i = 0; i < num_packets; i++)
159  pmp->packet_sizes[i] = avio_rl32(pb);
160  }
161  ret = av_get_packet(pb, pkt, pmp->packet_sizes[pmp->current_packet]);
162  if (ret >= 0) {
163  ret = 0;
164  pkt->stream_index = pmp->cur_stream;
165  }
166  if (pmp->current_packet % pmp->audio_packets == 0)
167  pmp->cur_stream = (pmp->cur_stream + 1) % pmp->num_streams;
168  pmp->current_packet++;
169  return ret;
170 }
171 
172 static int pmp_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
173 {
174  PMPContext *pmp = s->priv_data;
175  pmp->cur_stream = 0;
176  // fall back on default seek now
177  return -1;
178 }
179 
181 {
182  PMPContext *pmp = s->priv_data;
183  av_freep(&pmp->packet_sizes);
184  return 0;
185 }
186 
188  .p.name = "pmp",
189  .p.long_name = NULL_IF_CONFIG_SMALL("Playstation Portable PMP"),
190  .priv_data_size = sizeof(PMPContext),
194  .read_seek = pmp_seek,
196 };
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
ff_pmp_demuxer
const FFInputFormat ff_pmp_demuxer
Definition: pmpdec.c:187
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
PMPContext::audio_packets
int audio_packets
Definition: pmpdec.c:30
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:322
pmp_close
static int pmp_close(AVFormatContext *s)
Definition: pmpdec.c:180
AVINDEX_KEYFRAME
#define AVINDEX_KEYFRAME
Definition: avformat.h:610
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
avpriv_set_pts_info
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:853
read_seek
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:151
av_add_index_entry
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:120
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
AV_CODEC_ID_MP3
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: codec_id.h:441
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:802
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:713
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:41
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
PMPContext::cur_stream
int cur_stream
Definition: pmpdec.c:28
channels
channels
Definition: aptx.h:31
fsize
static int64_t fsize(FILE *f)
Definition: audiomatch.c:29
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
pmp_probe
static int pmp_probe(const AVProbeData *p)
Definition: pmpdec.c:36
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:550
NULL
#define NULL
Definition: coverity.c:32
AV_RN32
#define AV_RN32(p)
Definition: intreadwrite.h:362
PMPContext::num_streams
int num_streams
Definition: pmpdec.c:29
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:804
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:442
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:729
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
size
int size
Definition: twinvq_data.h:10344
PMPContext::packet_sizes
uint32_t * packet_sizes
Definition: pmpdec.c:32
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:602
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
pmp_header
static int pmp_header(AVFormatContext *s)
Definition: pmpdec.c:43
AVCodecParameters::height
int height
Definition: codec_par.h:135
demux.h
pmp_seek
static int pmp_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
Definition: pmpdec.c:172
av_get_packet
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:103
PMPContext::current_packet
int current_packet
Definition: pmpdec.c:31
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
AV_LOG_FATAL
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:174
pos
unsigned int pos
Definition: spdifenc.c:413
avformat.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVPacket::stream_index
int stream_index
Definition: packet.h:524
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:317
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
PMPContext
Definition: pmpdec.c:27
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:499
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_fast_malloc
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
FFInputFormat
Definition: demux.h:37
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
pmp_packet
static int pmp_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pmpdec.c:130
PMPContext::packet_sizes_alloc
int packet_sizes_alloc
Definition: pmpdec.c:33
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:345