FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "internal.h"
25 
26 typedef struct {
31  uint32_t *packet_sizes;
33 } PMPContext;
34 
35 static int pmp_probe(AVProbeData *p) {
36  if (AV_RN32(p->buf) == AV_RN32("pmpm") &&
37  AV_RL32(p->buf + 4) == 1)
38  return AVPROBE_SCORE_MAX;
39  return 0;
40 }
41 
43 {
44  PMPContext *pmp = s->priv_data;
45  AVIOContext *pb = s->pb;
46  int tb_num, tb_den;
47  uint32_t index_cnt;
48  int audio_codec_id = AV_CODEC_ID_NONE;
49  int srate, channels;
50  unsigned i;
51  uint64_t pos;
52  int64_t fsize = avio_size(pb);
53 
55  if (!vst)
56  return AVERROR(ENOMEM);
58  avio_skip(pb, 8);
59  switch (avio_rl32(pb)) {
60  case 0:
62  break;
63  case 1:
65  break;
66  default:
67  av_log(s, AV_LOG_ERROR, "Unsupported video format\n");
68  break;
69  }
70  index_cnt = avio_rl32(pb);
71  vst->codecpar->width = avio_rl32(pb);
72  vst->codecpar->height = avio_rl32(pb);
73 
74  tb_num = avio_rl32(pb);
75  tb_den = avio_rl32(pb);
76  avpriv_set_pts_info(vst, 32, tb_num, tb_den);
77  vst->nb_frames = index_cnt;
78  vst->duration = index_cnt;
79 
80  switch (avio_rl32(pb)) {
81  case 0:
82  audio_codec_id = AV_CODEC_ID_MP3;
83  break;
84  case 1:
85  av_log(s, AV_LOG_ERROR, "AAC not yet correctly supported\n");
86  audio_codec_id = AV_CODEC_ID_AAC;
87  break;
88  default:
89  av_log(s, AV_LOG_ERROR, "Unsupported audio format\n");
90  break;
91  }
92  pmp->num_streams = avio_rl16(pb) + 1;
93  avio_skip(pb, 10);
94  srate = avio_rl32(pb);
95  channels = avio_rl32(pb) + 1;
96  pos = avio_tell(pb) + 4LL*index_cnt;
97  for (i = 0; i < index_cnt; i++) {
98  uint32_t size = avio_rl32(pb);
99  int flags = size & 1 ? AVINDEX_KEYFRAME : 0;
100  if (avio_feof(pb)) {
101  av_log(s, AV_LOG_FATAL, "Encountered EOF while reading index.\n");
102  return AVERROR_INVALIDDATA;
103  }
104  size >>= 1;
105  if (size < 9 + 4*pmp->num_streams) {
106  av_log(s, AV_LOG_ERROR, "Packet too small\n");
107  return AVERROR_INVALIDDATA;
108  }
109  av_add_index_entry(vst, pos, i, size, 0, flags);
110  pos += size;
111  if (fsize > 0 && i == 0 && pos > fsize) {
112  av_log(s, AV_LOG_ERROR, "File ends before first packet\n");
113  return AVERROR_INVALIDDATA;
114  }
115  }
116  for (i = 1; i < pmp->num_streams; i++) {
117  AVStream *ast = avformat_new_stream(s, NULL);
118  if (!ast)
119  return AVERROR(ENOMEM);
121  ast->codecpar->codec_id = audio_codec_id;
122  ast->codecpar->channels = channels;
123  ast->codecpar->sample_rate = srate;
124  avpriv_set_pts_info(ast, 32, 1, srate);
125  }
126  return 0;
127 }
128 
130 {
131  PMPContext *pmp = s->priv_data;
132  AVIOContext *pb = s->pb;
133  int ret = 0;
134  int i;
135 
136  if (avio_feof(pb))
137  return AVERROR_EOF;
138  if (pmp->cur_stream == 0) {
139  int num_packets;
140  pmp->audio_packets = avio_r8(pb);
141 
142  if (!pmp->audio_packets) {
143  av_log(s, AV_LOG_ERROR, "No audio packets.\n");
144  return AVERROR_INVALIDDATA;
145  }
146 
147  num_packets = (pmp->num_streams - 1) * pmp->audio_packets + 1;
148  avio_skip(pb, 8);
149  pmp->current_packet = 0;
151  &pmp->packet_sizes_alloc,
152  num_packets * sizeof(*pmp->packet_sizes));
153  if (!pmp->packet_sizes_alloc) {
154  av_log(s, AV_LOG_ERROR, "Cannot (re)allocate packet buffer\n");
155  return AVERROR(ENOMEM);
156  }
157  for (i = 0; i < num_packets; i++)
158  pmp->packet_sizes[i] = avio_rl32(pb);
159  }
160  ret = av_get_packet(pb, pkt, pmp->packet_sizes[pmp->current_packet]);
161  if (ret >= 0) {
162  ret = 0;
163  pkt->stream_index = pmp->cur_stream;
164  }
165  if (pmp->current_packet % pmp->audio_packets == 0)
166  pmp->cur_stream = (pmp->cur_stream + 1) % pmp->num_streams;
167  pmp->current_packet++;
168  return ret;
169 }
170 
171 static int pmp_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
172 {
173  PMPContext *pmp = s->priv_data;
174  pmp->cur_stream = 0;
175  // fall back on default seek now
176  return -1;
177 }
178 
180 {
181  PMPContext *pmp = s->priv_data;
182  av_freep(&pmp->packet_sizes);
183  return 0;
184 }
185 
187  .name = "pmp",
188  .long_name = NULL_IF_CONFIG_SMALL("Playstation Portable PMP"),
189  .priv_data_size = sizeof(PMPContext),
193  .read_seek = pmp_seek,
195 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:147
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:309
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: utils.c:1945
uint32_t * packet_sizes
Definition: pmpdec.c:31
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4560
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3980
static int pmp_header(AVFormatContext *s)
Definition: pmpdec.c:42
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:304
int current_packet
Definition: pmpdec.c:30
static AVPacket pkt
Format I/O context.
Definition: avformat.h:1338
int width
Video only.
Definition: avcodec.h:4046
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4193
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
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:289
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:511
#define av_log(a,...)
#define AVINDEX_KEYFRAME
Definition: avformat.h:827
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int pmp_probe(AVProbeData *p)
Definition: pmpdec.c:35
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:726
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:517
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3976
static int pmp_close(AVFormatContext *s)
Definition: pmpdec.c:179
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:595
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:499
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:463
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int packet_sizes_alloc
Definition: pmpdec.c:32
int num_streams
Definition: pmpdec.c:28
int cur_stream
Definition: pmpdec.c:27
static int pmp_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
Definition: pmpdec.c:171
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:514
Stream structure.
Definition: avformat.h:889
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVIOContext * pb
I/O context.
Definition: avformat.h:1380
This structure contains the data a format has to probe a file.
Definition: avformat.h:461
#define AV_RN32(p)
Definition: intreadwrite.h:364
static int flags
Definition: cpu.c:47
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:943
int sample_rate
Audio only.
Definition: avcodec.h:4090
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:473
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:710
Main libavformat public API header.
static int pmp_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pmpdec.c:129
AVInputFormat ff_pmp_demuxer
Definition: pmpdec.c:186
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:945
void * priv_data
Format private data.
Definition: avformat.h:1366
int audio_packets
Definition: pmpdec.c:29
int channels
Audio only.
Definition: avcodec.h:4086
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:170
AVCodecParameters * codecpar
Definition: avformat.h:1241
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:328
static int64_t fsize(FILE *f)
Definition: audiomatch.c:28
int stream_index
Definition: avcodec.h:1603
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1578