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)
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 = AV_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 = AV_CODEC_ID_MPEG4;
00060 break;
00061 case 1:
00062 vst->codec->codec_id = AV_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 = AV_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 = AV_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 if (!pmp->audio_packets) {
00128 av_log_ask_for_sample(s, "0 audio packets\n");
00129 return AVERROR_PATCHWELCOME;
00130 }
00131 num_packets = (pmp->num_streams - 1) * pmp->audio_packets + 1;
00132 avio_skip(pb, 8);
00133 pmp->current_packet = 0;
00134 av_fast_malloc(&pmp->packet_sizes,
00135 &pmp->packet_sizes_alloc,
00136 num_packets * sizeof(*pmp->packet_sizes));
00137 if (!pmp->packet_sizes_alloc) {
00138 av_log(s, AV_LOG_ERROR, "Cannot (re)allocate packet buffer\n");
00139 return AVERROR(ENOMEM);
00140 }
00141 for (i = 0; i < num_packets; i++)
00142 pmp->packet_sizes[i] = avio_rl32(pb);
00143 }
00144 ret = av_get_packet(pb, pkt, pmp->packet_sizes[pmp->current_packet]);
00145 if (ret >= 0) {
00146 ret = 0;
00147
00148
00149 if (pmp->cur_stream == 0)
00150 pkt->dts = s->streams[0]->cur_dts++;
00151 pkt->stream_index = pmp->cur_stream;
00152 }
00153 if (pmp->current_packet % pmp->audio_packets == 0)
00154 pmp->cur_stream = (pmp->cur_stream + 1) % pmp->num_streams;
00155 pmp->current_packet++;
00156 return ret;
00157 }
00158
00159 static int pmp_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
00160 {
00161 PMPContext *pmp = s->priv_data;
00162 pmp->cur_stream = 0;
00163
00164 return -1;
00165 }
00166
00167 static int pmp_close(AVFormatContext *s)
00168 {
00169 PMPContext *pmp = s->priv_data;
00170 av_freep(&pmp->packet_sizes);
00171 return 0;
00172 }
00173
00174 AVInputFormat ff_pmp_demuxer = {
00175 .name = "pmp",
00176 .long_name = NULL_IF_CONFIG_SMALL("Playstation Portable PMP"),
00177 .priv_data_size = sizeof(PMPContext),
00178 .read_probe = pmp_probe,
00179 .read_header = pmp_header,
00180 .read_packet = pmp_packet,
00181 .read_seek = pmp_seek,
00182 .read_close = pmp_close,
00183 };