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.

196 lines
5.8KB

  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. #include "internal.h"
  24. typedef struct {
  25. int cur_stream;
  26. int num_streams;
  27. int audio_packets;
  28. int current_packet;
  29. uint32_t *packet_sizes;
  30. int packet_sizes_alloc;
  31. } PMPContext;
  32. static int pmp_probe(AVProbeData *p) {
  33. if (AV_RN32(p->buf) == AV_RN32("pmpm") &&
  34. AV_RL32(p->buf + 4) == 1)
  35. return AVPROBE_SCORE_MAX;
  36. return 0;
  37. }
  38. static int pmp_header(AVFormatContext *s)
  39. {
  40. PMPContext *pmp = s->priv_data;
  41. AVIOContext *pb = s->pb;
  42. int tb_num, tb_den;
  43. uint32_t index_cnt;
  44. int audio_codec_id = AV_CODEC_ID_NONE;
  45. int srate, channels;
  46. unsigned i;
  47. uint64_t pos;
  48. int64_t fsize = avio_size(pb);
  49. AVStream *vst = avformat_new_stream(s, NULL);
  50. if (!vst)
  51. return AVERROR(ENOMEM);
  52. vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  53. avio_skip(pb, 8);
  54. switch (avio_rl32(pb)) {
  55. case 0:
  56. vst->codec->codec_id = AV_CODEC_ID_MPEG4;
  57. break;
  58. case 1:
  59. vst->codec->codec_id = AV_CODEC_ID_H264;
  60. break;
  61. default:
  62. av_log(s, AV_LOG_ERROR, "Unsupported video format\n");
  63. break;
  64. }
  65. index_cnt = avio_rl32(pb);
  66. vst->codec->width = avio_rl32(pb);
  67. vst->codec->height = avio_rl32(pb);
  68. tb_num = avio_rl32(pb);
  69. tb_den = avio_rl32(pb);
  70. avpriv_set_pts_info(vst, 32, tb_num, tb_den);
  71. vst->nb_frames = index_cnt;
  72. vst->duration = index_cnt;
  73. switch (avio_rl32(pb)) {
  74. case 0:
  75. audio_codec_id = AV_CODEC_ID_MP3;
  76. break;
  77. case 1:
  78. av_log(s, AV_LOG_ERROR, "AAC not yet correctly supported\n");
  79. audio_codec_id = AV_CODEC_ID_AAC;
  80. break;
  81. default:
  82. av_log(s, AV_LOG_ERROR, "Unsupported audio format\n");
  83. break;
  84. }
  85. pmp->num_streams = avio_rl16(pb) + 1;
  86. avio_skip(pb, 10);
  87. srate = avio_rl32(pb);
  88. channels = avio_rl32(pb) + 1;
  89. pos = avio_tell(pb) + 4LL*index_cnt;
  90. for (i = 0; i < index_cnt; i++) {
  91. uint32_t size = avio_rl32(pb);
  92. int flags = size & 1 ? AVINDEX_KEYFRAME : 0;
  93. if (url_feof(pb)) {
  94. av_log(s, AV_LOG_FATAL, "Encountered EOF while reading index.\n");
  95. return AVERROR_INVALIDDATA;
  96. }
  97. size >>= 1;
  98. if (size < 9 + 4*pmp->num_streams) {
  99. av_log(s, AV_LOG_ERROR, "Packet too small\n");
  100. return AVERROR_INVALIDDATA;
  101. }
  102. av_add_index_entry(vst, pos, i, size, 0, flags);
  103. pos += size;
  104. if (fsize > 0 && i == 0 && pos > fsize) {
  105. av_log(s, AV_LOG_ERROR, "File ends before first packet\n");
  106. return AVERROR_INVALIDDATA;
  107. }
  108. }
  109. for (i = 1; i < pmp->num_streams; i++) {
  110. AVStream *ast = avformat_new_stream(s, NULL);
  111. if (!ast)
  112. return AVERROR(ENOMEM);
  113. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  114. ast->codec->codec_id = audio_codec_id;
  115. ast->codec->channels = channels;
  116. ast->codec->sample_rate = srate;
  117. avpriv_set_pts_info(ast, 32, 1, srate);
  118. }
  119. return 0;
  120. }
  121. static int pmp_packet(AVFormatContext *s, AVPacket *pkt)
  122. {
  123. PMPContext *pmp = s->priv_data;
  124. AVIOContext *pb = s->pb;
  125. int ret = 0;
  126. int i;
  127. if (url_feof(pb))
  128. return AVERROR_EOF;
  129. if (pmp->cur_stream == 0) {
  130. int num_packets;
  131. pmp->audio_packets = avio_r8(pb);
  132. if (!pmp->audio_packets) {
  133. av_log(s, AV_LOG_ERROR, "No audio packets.\n");
  134. return AVERROR_INVALIDDATA;
  135. }
  136. num_packets = (pmp->num_streams - 1) * pmp->audio_packets + 1;
  137. avio_skip(pb, 8);
  138. pmp->current_packet = 0;
  139. av_fast_malloc(&pmp->packet_sizes,
  140. &pmp->packet_sizes_alloc,
  141. num_packets * sizeof(*pmp->packet_sizes));
  142. if (!pmp->packet_sizes_alloc) {
  143. av_log(s, AV_LOG_ERROR, "Cannot (re)allocate packet buffer\n");
  144. return AVERROR(ENOMEM);
  145. }
  146. for (i = 0; i < num_packets; i++)
  147. pmp->packet_sizes[i] = avio_rl32(pb);
  148. }
  149. ret = av_get_packet(pb, pkt, pmp->packet_sizes[pmp->current_packet]);
  150. if (ret >= 0) {
  151. ret = 0;
  152. pkt->stream_index = pmp->cur_stream;
  153. }
  154. if (pmp->current_packet % pmp->audio_packets == 0)
  155. pmp->cur_stream = (pmp->cur_stream + 1) % pmp->num_streams;
  156. pmp->current_packet++;
  157. return ret;
  158. }
  159. static int pmp_seek(AVFormatContext *s, int stream_index, int64_t ts, int flags)
  160. {
  161. PMPContext *pmp = s->priv_data;
  162. pmp->cur_stream = 0;
  163. // fall back on default seek now
  164. return -1;
  165. }
  166. static int pmp_close(AVFormatContext *s)
  167. {
  168. PMPContext *pmp = s->priv_data;
  169. av_freep(&pmp->packet_sizes);
  170. return 0;
  171. }
  172. AVInputFormat ff_pmp_demuxer = {
  173. .name = "pmp",
  174. .long_name = NULL_IF_CONFIG_SMALL("Playstation Portable PMP"),
  175. .priv_data_size = sizeof(PMPContext),
  176. .read_probe = pmp_probe,
  177. .read_header = pmp_header,
  178. .read_packet = pmp_packet,
  179. .read_seek = pmp_seek,
  180. .read_close = pmp_close,
  181. };