OSDN Git Service

flvenc: use avcodec_get_name to report unsupported codecs.
[coroid/ffmpeg_saccubus.git] / libavformat / pmpdec.c
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
25 typedef struct {
26     int cur_stream;
27     int num_streams;
28     int audio_packets;
29     int current_packet;
30     uint32_t *packet_sizes;
31     int packet_sizes_alloc;
32 } PMPContext;
33
34 static int pmp_probe(AVProbeData *p) {
35     if (AV_RN32(p->buf) == AV_RN32("pmpm") &&
36         AV_RL32(p->buf + 4) == 1)
37         return AVPROBE_SCORE_MAX;
38     return 0;
39 }
40
41 static int pmp_header(AVFormatContext *s, AVFormatParameters *ap) {
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 = av_new_stream(s, 0);
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_ERROR, "AAC 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 = av_new_stream(s, i);
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     PMPContext *pmp = s->priv_data;
115     AVIOContext *pb = s->pb;
116     int ret = 0;
117     int i;
118
119     if (url_feof(pb))
120         return AVERROR_EOF;
121     if (pmp->cur_stream == 0) {
122         int num_packets;
123         pmp->audio_packets = avio_r8(pb);
124         num_packets = (pmp->num_streams - 1) * pmp->audio_packets + 1;
125         avio_skip(pb, 8);
126         pmp->current_packet = 0;
127         av_fast_malloc(&pmp->packet_sizes,
128                        &pmp->packet_sizes_alloc,
129                        num_packets * sizeof(*pmp->packet_sizes));
130         for (i = 0; i < num_packets; i++)
131             pmp->packet_sizes[i] = avio_rl32(pb);
132     }
133     ret = av_get_packet(pb, pkt, pmp->packet_sizes[pmp->current_packet]);
134     if (ret >= 0) {
135         ret = 0;
136         // FIXME: this is a hack that should be remove once
137         // compute_pkt_fields can handle
138         if (pmp->cur_stream == 0)
139             pkt->dts = s->streams[0]->cur_dts++;
140         pkt->stream_index = pmp->cur_stream;
141     }
142     if (pmp->current_packet % pmp->audio_packets == 0)
143         pmp->cur_stream = (pmp->cur_stream + 1) % pmp->num_streams;
144     pmp->current_packet++;
145     return ret;
146 }
147
148 static int pmp_seek(AVFormatContext *s, int stream_index,
149                      int64_t ts, int flags) {
150     PMPContext *pmp = s->priv_data;
151     pmp->cur_stream = 0;
152     // fallback to default seek now
153     return -1;
154 }
155
156 static int pmp_close(AVFormatContext *s)
157 {
158     PMPContext *pmp = s->priv_data;
159     av_freep(&pmp->packet_sizes);
160     return 0;
161 }
162
163 AVInputFormat ff_pmp_demuxer = {
164     .name           = "pmp",
165     .long_name      = NULL_IF_CONFIG_SMALL("Playstation Portable PMP format"),
166     .priv_data_size = sizeof(PMPContext),
167     .read_probe     = pmp_probe,
168     .read_header    = pmp_header,
169     .read_packet    = pmp_packet,
170     .read_seek      = pmp_seek,
171     .read_close     = pmp_close,
172 };