pmpdec.c 5.3 KB

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