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