You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
5.3KB

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