pmpdec.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #include "libavutil/intreadwrite.h"
  22. #include "avformat.h"
  23. typedef struct {
  24. int cur_stream;
  25. int num_streams;
  26. int audio_packets;
  27. int current_packet;
  28. uint32_t *packet_sizes;
  29. int packet_sizes_alloc;
  30. } PMPContext;
  31. static int pmp_probe(AVProbeData *p) {
  32. if (AV_RN32(p->buf) == AV_RN32("pmpm") &&
  33. AV_RL32(p->buf + 4) == 1)
  34. return AVPROBE_SCORE_MAX;
  35. return 0;
  36. }
  37. static int pmp_header(AVFormatContext *s, AVFormatParameters *ap) {
  38. PMPContext *pmp = s->priv_data;
  39. AVIOContext *pb = s->pb;
  40. int tb_num, tb_den;
  41. int index_cnt;
  42. int audio_codec_id = CODEC_ID_NONE;
  43. int srate, channels;
  44. int i;
  45. uint64_t pos;
  46. AVStream *vst = av_new_stream(s, 0);
  47. if (!vst)
  48. return AVERROR(ENOMEM);
  49. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  50. avio_skip(pb, 8);
  51. switch (avio_rl32(pb)) {
  52. case 0:
  53. vst->codec->codec_id = CODEC_ID_MPEG4;
  54. break;
  55. case 1:
  56. vst->codec->codec_id = CODEC_ID_H264;
  57. break;
  58. default:
  59. av_log(s, AV_LOG_ERROR, "Unsupported video format\n");
  60. break;
  61. }
  62. index_cnt = avio_rl32(pb);
  63. vst->codec->width = avio_rl32(pb);
  64. vst->codec->height = avio_rl32(pb);
  65. tb_num = avio_rl32(pb);
  66. tb_den = avio_rl32(pb);
  67. av_set_pts_info(vst, 32, tb_num, tb_den);
  68. vst->nb_frames = index_cnt;
  69. vst->duration = index_cnt;
  70. switch (avio_rl32(pb)) {
  71. case 0:
  72. audio_codec_id = CODEC_ID_MP3;
  73. break;
  74. case 1:
  75. av_log(s, AV_LOG_ERROR, "AAC not yet correctly supported\n");
  76. audio_codec_id = CODEC_ID_AAC;
  77. break;
  78. default:
  79. av_log(s, AV_LOG_ERROR, "Unsupported audio format\n");
  80. break;
  81. }
  82. pmp->num_streams = avio_rl16(pb) + 1;
  83. avio_skip(pb, 10);
  84. srate = avio_rl32(pb);
  85. channels = avio_rl32(pb) + 1;
  86. for (i = 1; i < pmp->num_streams; i++) {
  87. AVStream *ast = av_new_stream(s, i);
  88. if (!ast)
  89. return AVERROR(ENOMEM);
  90. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  91. ast->codec->codec_id = audio_codec_id;
  92. ast->codec->channels = channels;
  93. ast->codec->sample_rate = srate;
  94. av_set_pts_info(ast, 32, 1, srate);
  95. }
  96. pos = avio_tell(pb) + 4*index_cnt;
  97. for (i = 0; i < index_cnt; i++) {
  98. int size = avio_rl32(pb);
  99. int flags = size & 1 ? AVINDEX_KEYFRAME : 0;
  100. size >>= 1;
  101. av_add_index_entry(vst, pos, i, size, 0, flags);
  102. pos += size;
  103. }
  104. return 0;
  105. }
  106. static int pmp_packet(AVFormatContext *s, AVPacket *pkt) {
  107. PMPContext *pmp = s->priv_data;
  108. AVIOContext *pb = s->pb;
  109. int ret = 0;
  110. int i;
  111. if (url_feof(pb))
  112. return AVERROR_EOF;
  113. if (pmp->cur_stream == 0) {
  114. int num_packets;
  115. pmp->audio_packets = avio_r8(pb);
  116. num_packets = (pmp->num_streams - 1) * pmp->audio_packets + 1;
  117. avio_skip(pb, 8);
  118. pmp->current_packet = 0;
  119. av_fast_malloc(&pmp->packet_sizes,
  120. &pmp->packet_sizes_alloc,
  121. num_packets * sizeof(*pmp->packet_sizes));
  122. for (i = 0; i < num_packets; i++)
  123. pmp->packet_sizes[i] = avio_rl32(pb);
  124. }
  125. ret = av_get_packet(pb, pkt, pmp->packet_sizes[pmp->current_packet]);
  126. if (ret >= 0) {
  127. ret = 0;
  128. // FIXME: this is a hack that should be remove once
  129. // compute_pkt_fields can handle
  130. if (pmp->cur_stream == 0)
  131. pkt->dts = s->streams[0]->cur_dts++;
  132. pkt->stream_index = pmp->cur_stream;
  133. }
  134. if (pmp->current_packet % pmp->audio_packets == 0)
  135. pmp->cur_stream = (pmp->cur_stream + 1) % pmp->num_streams;
  136. pmp->current_packet++;
  137. return ret;
  138. }
  139. static int pmp_seek(AVFormatContext *s, int stream_index,
  140. int64_t ts, int flags) {
  141. PMPContext *pmp = s->priv_data;
  142. pmp->cur_stream = 0;
  143. // fallback to default seek now
  144. return -1;
  145. }
  146. static int pmp_close(AVFormatContext *s)
  147. {
  148. PMPContext *pmp = s->priv_data;
  149. av_freep(&pmp->packet_sizes);
  150. return 0;
  151. }
  152. AVInputFormat ff_pmp_demuxer = {
  153. .name = "pmp",
  154. .long_name = NULL_IF_CONFIG_SMALL("Playstation Portable PMP format"),
  155. .priv_data_size = sizeof(PMPContext),
  156. .read_probe = pmp_probe,
  157. .read_header = pmp_header,
  158. .read_packet = pmp_packet,
  159. .read_seek = pmp_seek,
  160. .read_close = pmp_close,
  161. };