• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/pmpdec.c

Go to the documentation of this file.
00001 /*
00002  * PMP demuxer
00003  * Copyright (c) 2011 Reimar Döffinger
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "libavutil/intreadwrite.h"
00023 #include "avformat.h"
00024 #include "internal.h"
00025 
00026 typedef struct PMPContext {
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 {
00037     if (!memcmp(p->buf, "pmpm\1\0\0\0", 8))
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_WARNING, "AAC is 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 (pb->eof_reached)
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         // FIXME: this is a hack that should be removed once
00144         // compute_pkt_fields() can handle timestamps properly
00145         if (pmp->cur_stream == 0)
00146             pkt->dts = s->streams[0]->cur_dts++;
00147         pkt->stream_index = pmp->cur_stream;
00148     }
00149     pmp->current_packet++;
00150     if (pmp->current_packet == 1 || pmp->current_packet > pmp->audio_packets)
00151         pmp->cur_stream = (pmp->cur_stream + 1) % pmp->num_streams;
00152 
00153     return ret;
00154 }
00155 
00156 static int pmp_seek(AVFormatContext *s, int stream_idx, int64_t ts, int flags)
00157 {
00158     PMPContext *pmp = s->priv_data;
00159     pmp->cur_stream = 0;
00160     // fallback to default seek now
00161     return -1;
00162 }
00163 
00164 static int pmp_close(AVFormatContext *s)
00165 {
00166     PMPContext *pmp = s->priv_data;
00167     av_freep(&pmp->packet_sizes);
00168     return 0;
00169 }
00170 
00171 AVInputFormat ff_pmp_demuxer = {
00172     .name           = "pmp",
00173     .long_name      = NULL_IF_CONFIG_SMALL("Playstation Portable PMP format"),
00174     .priv_data_size = sizeof(PMPContext),
00175     .read_probe     = pmp_probe,
00176     .read_header    = pmp_header,
00177     .read_packet    = pmp_packet,
00178     .read_seek      = pmp_seek,
00179     .read_close     = pmp_close,
00180 };
Generated on Thu Jan 24 2013 17:08:55 for Libav by doxygen 1.7.1