Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 PMPContext {
31  uint32_t *packet_sizes;
33 } PMPContext;
34 
35 static int pmp_probe(AVProbeData *p)
36 {
37  if (!memcmp(p->buf, "pmpm\1\0\0\0", 8))
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  int index_cnt;
48  int audio_codec_id = AV_CODEC_ID_NONE;
49  int srate, channels;
50  int i;
51  uint64_t pos;
53  if (!vst)
54  return AVERROR(ENOMEM);
56  avio_skip(pb, 8);
57  switch (avio_rl32(pb)) {
58  case 0:
60  break;
61  case 1:
63  break;
64  default:
65  av_log(s, AV_LOG_ERROR, "Unsupported video format\n");
66  break;
67  }
68  index_cnt = avio_rl32(pb);
69  vst->codec->width = avio_rl32(pb);
70  vst->codec->height = avio_rl32(pb);
71 
72  tb_num = avio_rl32(pb);
73  tb_den = avio_rl32(pb);
74  avpriv_set_pts_info(vst, 32, tb_num, tb_den);
75  vst->nb_frames = index_cnt;
76  vst->duration = index_cnt;
77 
78  switch (avio_rl32(pb)) {
79  case 0:
80  audio_codec_id = AV_CODEC_ID_MP3;
81  break;
82  case 1:
83  av_log(s, AV_LOG_WARNING, "AAC is not yet correctly supported\n");
84  audio_codec_id = AV_CODEC_ID_AAC;
85  break;
86  default:
87  av_log(s, AV_LOG_ERROR, "Unsupported audio format\n");
88  break;
89  }
90  pmp->num_streams = avio_rl16(pb) + 1;
91  avio_skip(pb, 10);
92  srate = avio_rl32(pb);
93  channels = avio_rl32(pb) + 1;
94  for (i = 1; i < pmp->num_streams; i++) {
96  if (!ast)
97  return AVERROR(ENOMEM);
99  ast->codec->codec_id = audio_codec_id;
100  ast->codec->channels = channels;
101  ast->codec->sample_rate = srate;
102  avpriv_set_pts_info(ast, 32, 1, srate);
103  }
104  pos = avio_tell(pb) + 4 * index_cnt;
105  for (i = 0; i < index_cnt; i++) {
106  int size = avio_rl32(pb);
107  int flags = size & 1 ? AVINDEX_KEYFRAME : 0;
108  size >>= 1;
109  av_add_index_entry(vst, pos, i, size, 0, flags);
110  pos += size;
111  }
112  return 0;
113 }
114 
115 static int pmp_packet(AVFormatContext *s, AVPacket *pkt)
116 {
117  PMPContext *pmp = s->priv_data;
118  AVIOContext *pb = s->pb;
119  int ret = 0;
120  int i;
121 
122  if (pb->eof_reached)
123  return AVERROR_EOF;
124  if (pmp->cur_stream == 0) {
125  int num_packets;
126  pmp->audio_packets = avio_r8(pb);
127  if (!pmp->audio_packets) {
128  av_log(s, AV_LOG_ERROR, "No audio packets.\n");
129  return AVERROR_INVALIDDATA;
130  }
131 
132  num_packets = (pmp->num_streams - 1) * pmp->audio_packets + 1;
133  avio_skip(pb, 8);
134  pmp->current_packet = 0;
136  &pmp->packet_sizes_alloc,
137  num_packets * sizeof(*pmp->packet_sizes));
138  if (!pmp->packet_sizes_alloc) {
139  av_log(s, AV_LOG_ERROR, "Cannot (re)allocate packet buffer\n");
140  return AVERROR(ENOMEM);
141  }
142  for (i = 0; i < num_packets; i++)
143  pmp->packet_sizes[i] = avio_rl32(pb);
144  }
145  ret = av_get_packet(pb, pkt, pmp->packet_sizes[pmp->current_packet]);
146  if (ret > 0) {
147  ret = 0;
148  // FIXME: this is a hack that should be removed once
149  // compute_pkt_fields() can handle timestamps properly
150  if (pmp->cur_stream == 0)
151  pkt->dts = s->streams[0]->cur_dts++;
152  pkt->stream_index = pmp->cur_stream;
153  }
154  pmp->current_packet++;
155  if (pmp->current_packet == 1 || pmp->current_packet > pmp->audio_packets)
156  pmp->cur_stream = (pmp->cur_stream + 1) % pmp->num_streams;
157 
158  return ret;
159 }
160 
161 static int pmp_seek(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
162 {
163  PMPContext *pmp = s->priv_data;
164  pmp->cur_stream = 0;
165  // fall back on default seek now
166  return -1;
167 }
168 
170 {
171  PMPContext *pmp = s->priv_data;
172  av_freep(&pmp->packet_sizes);
173  return 0;
174 }
175 
177  .name = "pmp",
178  .long_name = NULL_IF_CONFIG_SMALL("Playstation Portable PMP"),
179  .priv_data_size = sizeof(PMPContext),
183  .read_seek = pmp_seek,
185 };
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int size
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:1312
uint32_t * packet_sizes
Definition: pmpdec.c:31
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
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:3208
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
static int pmp_seek(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
Definition: pmpdec.c:161
static int pmp_header(AVFormatContext *s)
Definition: pmpdec.c:42
int current_packet
Definition: pmpdec.c:30
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
Format I/O context.
Definition: avformat.h:871
int64_t cur_dts
Definition: avformat.h:800
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:935
static int flags
Definition: log.c:44
#define AVERROR_EOF
End of file.
Definition: error.h:51
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:118
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
#define AVINDEX_KEYFRAME
Definition: avformat.h:646
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
static int pmp_probe(AVProbeData *p)
Definition: pmpdec.c:35
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:558
#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:142
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:369
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2662
static int pmp_close(AVFormatContext *s)
Definition: pmpdec.c:169
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:437
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:702
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:390
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1217
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
int packet_sizes_alloc
Definition: pmpdec.c:32
int num_streams
Definition: pmpdec.c:28
int cur_stream
Definition: pmpdec.c:27
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:536
Stream structure.
Definition: avformat.h:683
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1062
enum AVCodecID codec_id
Definition: avcodec.h:1065
int sample_rate
samples per second
Definition: avcodec.h:1779
AVIOContext * pb
I/O context.
Definition: avformat.h:913
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
This structure contains the data a format has to probe a file.
Definition: avformat.h:388
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:734
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:395
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:542
Main libavformat public API header.
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:368
static int pmp_packet(AVFormatContext *s, AVPacket *pkt)
Definition: pmpdec.c:115
AVInputFormat ff_pmp_demuxer
Definition: pmpdec.c:176
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:736
int eof_reached
true if eof reached
Definition: avio.h:96
int channels
number of audio channels
Definition: avcodec.h:1780
void * priv_data
Format private data.
Definition: avformat.h:899
int audio_packets
Definition: pmpdec.c:29
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:972
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:516
int stream_index
Definition: avcodec.h:975
This structure stores compressed data.
Definition: avcodec.h:950