Browse code

Add PlayStation Portable PMP format demuxer

Not yet complete, for demuxing AAC the AAC header must be generated
manually.
Possibly the decoder could accept the header as extradata to simplify
this.

Signed-off-by: Anton Khirnov <anton@khirnov.net>

Reimar Döffinger authored on 2011/03/27 00:25:10
Showing 5 changed files
... ...
@@ -103,6 +103,7 @@ easier to use. The changes are:
103 103
 - VBLE Decoder
104 104
 - OS X Video Decoder Acceleration (VDA) support
105 105
 - CRI ADX audio format demuxer
106
+- Playstation Portable PMP format demuxer
106 107
 
107 108
 
108 109
 version 0.7:
... ...
@@ -167,6 +167,7 @@ library:
167 167
 @item NUT                       @tab X @tab X
168 168
     @tab NUT Open Container Format
169 169
 @item Ogg                       @tab X @tab X
170
+@item Playstation Portable PMP  @tab   @tab X
170 171
 @item TechnoTrend PVA           @tab   @tab X
171 172
     @tab Used by TechnoTrend DVB PCI boards.
172 173
 @item QCP                       @tab   @tab X
... ...
@@ -218,6 +218,7 @@ OBJS-$(CONFIG_PCM_U32LE_DEMUXER)         += pcmdec.o pcm.o rawdec.o
218 218
 OBJS-$(CONFIG_PCM_U32LE_MUXER)           += pcmenc.o rawenc.o
219 219
 OBJS-$(CONFIG_PCM_U8_DEMUXER)            += pcmdec.o pcm.o rawdec.o
220 220
 OBJS-$(CONFIG_PCM_U8_MUXER)              += pcmenc.o rawenc.o
221
+OBJS-$(CONFIG_PMP_DEMUXER)               += pmpdec.o
221 222
 OBJS-$(CONFIG_PVA_DEMUXER)               += pva.o
222 223
 OBJS-$(CONFIG_QCP_DEMUXER)               += qcp.o
223 224
 OBJS-$(CONFIG_R3D_DEMUXER)               += r3d.o
... ...
@@ -177,6 +177,7 @@ void av_register_all(void)
177 177
     REGISTER_MUXDEMUX (PCM_U16BE, pcm_u16be);
178 178
     REGISTER_MUXDEMUX (PCM_U16LE, pcm_u16le);
179 179
     REGISTER_MUXDEMUX (PCM_U8,    pcm_u8);
180
+    REGISTER_DEMUXER  (PMP, pmp);
180 181
     REGISTER_MUXER    (PSP, psp);
181 182
     REGISTER_DEMUXER  (PVA, pva);
182 183
     REGISTER_DEMUXER  (QCP, qcp);
183 184
new file mode 100644
... ...
@@ -0,0 +1,179 @@
0
+/*
1
+ * PMP demuxer
2
+ * Copyright (c) 2011 Reimar Döffinger
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * Libav is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with Libav; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "libavutil/intreadwrite.h"
22
+#include "avformat.h"
23
+
24
+typedef struct PMPContext {
25
+    int       cur_stream;
26
+    int       num_streams;
27
+    int       audio_packets;
28
+    int       current_packet;
29
+    uint32_t *packet_sizes;
30
+    int       packet_sizes_alloc;
31
+} PMPContext;
32
+
33
+static int pmp_probe(AVProbeData *p)
34
+{
35
+    if (!memcmp(p->buf, "pmpm\1\0\0\0", 8))
36
+        return AVPROBE_SCORE_MAX;
37
+    return 0;
38
+}
39
+
40
+static int pmp_header(AVFormatContext *s, AVFormatParameters *ap)
41
+{
42
+    PMPContext *pmp = s->priv_data;
43
+    AVIOContext *pb = s->pb;
44
+    int tb_num, tb_den;
45
+    int index_cnt;
46
+    int audio_codec_id = CODEC_ID_NONE;
47
+    int srate, channels;
48
+    int i;
49
+    uint64_t pos;
50
+    AVStream *vst = avformat_new_stream(s, NULL);
51
+    if (!vst)
52
+        return AVERROR(ENOMEM);
53
+    vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
54
+    avio_skip(pb, 8);
55
+    switch (avio_rl32(pb)) {
56
+    case 0:
57
+        vst->codec->codec_id = CODEC_ID_MPEG4;
58
+        break;
59
+    case 1:
60
+        vst->codec->codec_id = CODEC_ID_H264;
61
+        break;
62
+    default:
63
+        av_log(s, AV_LOG_ERROR, "Unsupported video format\n");
64
+        break;
65
+    }
66
+    index_cnt          = avio_rl32(pb);
67
+    vst->codec->width  = avio_rl32(pb);
68
+    vst->codec->height = avio_rl32(pb);
69
+
70
+    tb_num = avio_rl32(pb);
71
+    tb_den = avio_rl32(pb);
72
+    av_set_pts_info(vst, 32, tb_num, tb_den);
73
+    vst->nb_frames = index_cnt;
74
+    vst->duration  = index_cnt;
75
+
76
+    switch (avio_rl32(pb)) {
77
+    case 0:
78
+        audio_codec_id = CODEC_ID_MP3;
79
+        break;
80
+    case 1:
81
+        av_log(s, AV_LOG_WARNING, "AAC is not yet correctly supported\n");
82
+        audio_codec_id = CODEC_ID_AAC;
83
+        break;
84
+    default:
85
+        av_log(s, AV_LOG_ERROR, "Unsupported audio format\n");
86
+        break;
87
+    }
88
+    pmp->num_streams = avio_rl16(pb) + 1;
89
+    avio_skip(pb, 10);
90
+    srate    = avio_rl32(pb);
91
+    channels = avio_rl32(pb) + 1;
92
+    for (i = 1; i < pmp->num_streams; i++) {
93
+        AVStream *ast = avformat_new_stream(s, NULL);
94
+        if (!ast)
95
+            return AVERROR(ENOMEM);
96
+        ast->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
97
+        ast->codec->codec_id    = audio_codec_id;
98
+        ast->codec->channels    = channels;
99
+        ast->codec->sample_rate = srate;
100
+        av_set_pts_info(ast, 32, 1, srate);
101
+    }
102
+    pos = avio_tell(pb) + 4 * index_cnt;
103
+    for (i = 0; i < index_cnt; i++) {
104
+        int size  = avio_rl32(pb);
105
+        int flags = size & 1 ? AVINDEX_KEYFRAME : 0;
106
+        size >>= 1;
107
+        av_add_index_entry(vst, pos, i, size, 0, flags);
108
+        pos += size;
109
+    }
110
+    return 0;
111
+}
112
+
113
+static int pmp_packet(AVFormatContext *s, AVPacket *pkt)
114
+{
115
+    PMPContext *pmp = s->priv_data;
116
+    AVIOContext *pb = s->pb;
117
+    int ret = 0;
118
+    int i;
119
+
120
+    if (pb->eof_reached)
121
+        return AVERROR_EOF;
122
+    if (pmp->cur_stream == 0) {
123
+        int num_packets;
124
+        pmp->audio_packets = avio_r8(pb);
125
+        num_packets = (pmp->num_streams - 1) * pmp->audio_packets + 1;
126
+        avio_skip(pb, 8);
127
+        pmp->current_packet = 0;
128
+        av_fast_malloc(&pmp->packet_sizes,
129
+                       &pmp->packet_sizes_alloc,
130
+                       num_packets * sizeof(*pmp->packet_sizes));
131
+        if (!pmp->packet_sizes_alloc) {
132
+            av_log(s, AV_LOG_ERROR, "Cannot (re)allocate packet buffer\n");
133
+            return AVERROR(ENOMEM);
134
+        }
135
+        for (i = 0; i < num_packets; i++)
136
+            pmp->packet_sizes[i] = avio_rl32(pb);
137
+    }
138
+    ret = av_get_packet(pb, pkt, pmp->packet_sizes[pmp->current_packet]);
139
+    if (ret > 0) {
140
+        ret = 0;
141
+        // FIXME: this is a hack that should be removed once
142
+        // compute_pkt_fields() can handle timestamps properly
143
+        if (pmp->cur_stream == 0)
144
+            pkt->dts = s->streams[0]->cur_dts++;
145
+        pkt->stream_index = pmp->cur_stream;
146
+    }
147
+    pmp->current_packet++;
148
+    if (pmp->current_packet == 1 || pmp->current_packet > pmp->audio_packets)
149
+        pmp->cur_stream = (pmp->cur_stream + 1) % pmp->num_streams;
150
+
151
+    return ret;
152
+}
153
+
154
+static int pmp_seek(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
155
+{
156
+    PMPContext *pmp = s->priv_data;
157
+    pmp->cur_stream = 0;
158
+    // fallback to default seek now
159
+    return -1;
160
+}
161
+
162
+static int pmp_close(AVFormatContext *s)
163
+{
164
+    PMPContext *pmp = s->priv_data;
165
+    av_freep(&pmp->packet_sizes);
166
+    return 0;
167
+}
168
+
169
+AVInputFormat ff_pmp_demuxer = {
170
+    .name           = "pmp",
171
+    .long_name      = NULL_IF_CONFIG_SMALL("Playstation Portable PMP format"),
172
+    .priv_data_size = sizeof(PMPContext),
173
+    .read_probe     = pmp_probe,
174
+    .read_header    = pmp_header,
175
+    .read_packet    = pmp_packet,
176
+    .read_seek      = pmp_seek,
177
+    .read_close     = pmp_close,
178
+};