00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "avformat.h"
00024
00025 typedef struct {
00026 int cur_stream;
00027 int num_streams;
00028 int audio_packets;
00029 int current_packet;
00030 uint32_t *packet_sizes;
00031 int packet_sizes_alloc;
00032 } PMPContext;
00033
00034 static int pmp_probe(AVProbeData *p) {
00035 if (AV_RN32(p->buf) == AV_RN32("pmpm") &&
00036 AV_RL32(p->buf + 4) == 1)
00037 return AVPROBE_SCORE_MAX;
00038 return 0;
00039 }
00040
00041 static int pmp_header(AVFormatContext *s, AVFormatParameters *ap) {
00042 PMPContext *pmp = s->priv_data;
00043 AVIOContext *pb = s->pb;
00044 int tb_num, tb_den;
00045 int index_cnt;
00046 int audio_codec_id = CODEC_ID_NONE;
00047 int srate, channels;
00048 int i;
00049 uint64_t pos;
00050 AVStream *vst = av_new_stream(s, 0);
00051 if (!vst)
00052 return AVERROR(ENOMEM);
00053 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00054 avio_skip(pb, 8);
00055 switch (avio_rl32(pb)) {
00056 case 0:
00057 vst->codec->codec_id = CODEC_ID_MPEG4;
00058 break;
00059 case 1:
00060 vst->codec->codec_id = CODEC_ID_H264;
00061 break;
00062 default:
00063 av_log(s, AV_LOG_ERROR, "Unsupported video format\n");
00064 break;
00065 }
00066 index_cnt = avio_rl32(pb);
00067 vst->codec->width = avio_rl32(pb);
00068 vst->codec->height = avio_rl32(pb);
00069
00070 tb_num = avio_rl32(pb);
00071 tb_den = avio_rl32(pb);
00072 av_set_pts_info(vst, 32, tb_num, tb_den);
00073 vst->nb_frames = index_cnt;
00074 vst->duration = index_cnt;
00075
00076 switch (avio_rl32(pb)) {
00077 case 0:
00078 audio_codec_id = CODEC_ID_MP3;
00079 break;
00080 case 1:
00081 av_log(s, AV_LOG_ERROR, "AAC not yet correctly supported\n");
00082 audio_codec_id = CODEC_ID_AAC;
00083 break;
00084 default:
00085 av_log(s, AV_LOG_ERROR, "Unsupported audio format\n");
00086 break;
00087 }
00088 pmp->num_streams = avio_rl16(pb) + 1;
00089 avio_skip(pb, 10);
00090 srate = avio_rl32(pb);
00091 channels = avio_rl32(pb) + 1;
00092 for (i = 1; i < pmp->num_streams; i++) {
00093 AVStream *ast = av_new_stream(s, i);
00094 if (!ast)
00095 return AVERROR(ENOMEM);
00096 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00097 ast->codec->codec_id = audio_codec_id;
00098 ast->codec->channels = channels;
00099 ast->codec->sample_rate = srate;
00100 av_set_pts_info(ast, 32, 1, srate);
00101 }
00102 pos = avio_tell(pb) + 4*index_cnt;
00103 for (i = 0; i < index_cnt; i++) {
00104 int size = avio_rl32(pb);
00105 int flags = size & 1 ? AVINDEX_KEYFRAME : 0;
00106 size >>= 1;
00107 av_add_index_entry(vst, pos, i, size, 0, flags);
00108 pos += size;
00109 }
00110 return 0;
00111 }
00112
00113 static int pmp_packet(AVFormatContext *s, AVPacket *pkt) {
00114 PMPContext *pmp = s->priv_data;
00115 AVIOContext *pb = s->pb;
00116 int ret = 0;
00117 int i;
00118
00119 if (url_feof(pb))
00120 return AVERROR_EOF;
00121 if (pmp->cur_stream == 0) {
00122 int num_packets;
00123 pmp->audio_packets = avio_r8(pb);
00124 num_packets = (pmp->num_streams - 1) * pmp->audio_packets + 1;
00125 avio_skip(pb, 8);
00126 pmp->current_packet = 0;
00127 av_fast_malloc(&pmp->packet_sizes,
00128 &pmp->packet_sizes_alloc,
00129 num_packets * sizeof(*pmp->packet_sizes));
00130 for (i = 0; i < num_packets; i++)
00131 pmp->packet_sizes[i] = avio_rl32(pb);
00132 }
00133 ret = av_get_packet(pb, pkt, pmp->packet_sizes[pmp->current_packet]);
00134 if (ret >= 0) {
00135 ret = 0;
00136
00137
00138 if (pmp->cur_stream == 0)
00139 pkt->dts = s->streams[0]->cur_dts++;
00140 pkt->stream_index = pmp->cur_stream;
00141 }
00142 if (pmp->current_packet % pmp->audio_packets == 0)
00143 pmp->cur_stream = (pmp->cur_stream + 1) % pmp->num_streams;
00144 pmp->current_packet++;
00145 return ret;
00146 }
00147
00148 static int pmp_seek(AVFormatContext *s, int stream_index,
00149 int64_t ts, int flags) {
00150 PMPContext *pmp = s->priv_data;
00151 pmp->cur_stream = 0;
00152
00153 return -1;
00154 }
00155
00156 static int pmp_close(AVFormatContext *s)
00157 {
00158 PMPContext *pmp = s->priv_data;
00159 av_freep(&pmp->packet_sizes);
00160 return 0;
00161 }
00162
00163 AVInputFormat ff_pmp_demuxer = {
00164 .name = "pmp",
00165 .long_name = NULL_IF_CONFIG_SMALL("Playstation Portable PMP format"),
00166 .priv_data_size = sizeof(PMPContext),
00167 .read_probe = pmp_probe,
00168 .read_header = pmp_header,
00169 .read_packet = pmp_packet,
00170 .read_seek = pmp_seek,
00171 .read_close = pmp_close,
00172 };