pva.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * TechnoTrend PVA (.pva) demuxer
  3. * Copyright (c) 2007, 2008 Ivo van Poorten
  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 "avformat.h"
  22. #include "mpeg.h"
  23. #define PVA_MAX_PAYLOAD_LENGTH 0x17f8
  24. #define PVA_VIDEO_PAYLOAD 0x01
  25. #define PVA_AUDIO_PAYLOAD 0x02
  26. #define PVA_MAGIC (('A' << 8) + 'V')
  27. typedef struct {
  28. int continue_pes;
  29. } PVAContext;
  30. static int pva_probe(AVProbeData * pd) {
  31. unsigned char *buf = pd->buf;
  32. if (AV_RB16(buf) == PVA_MAGIC && buf[2] && buf[2] < 3 && buf[4] == 0x55)
  33. return AVPROBE_SCORE_MAX / 2;
  34. return 0;
  35. }
  36. static int pva_read_header(AVFormatContext *s, AVFormatParameters *ap) {
  37. AVStream *st;
  38. if (!(st = av_new_stream(s, 0)))
  39. return AVERROR(ENOMEM);
  40. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  41. st->codec->codec_id = CODEC_ID_MPEG2VIDEO;
  42. st->need_parsing = AVSTREAM_PARSE_FULL;
  43. av_set_pts_info(st, 32, 1, 90000);
  44. av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
  45. if (!(st = av_new_stream(s, 1)))
  46. return AVERROR(ENOMEM);
  47. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  48. st->codec->codec_id = CODEC_ID_MP2;
  49. st->need_parsing = AVSTREAM_PARSE_FULL;
  50. av_set_pts_info(st, 33, 1, 90000);
  51. av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
  52. /* the parameters will be extracted from the compressed bitstream */
  53. return 0;
  54. }
  55. #define pva_log if (read_packet) av_log
  56. static int read_part_of_packet(AVFormatContext *s, int64_t *pts,
  57. int *len, int *strid, int read_packet) {
  58. AVIOContext *pb = s->pb;
  59. PVAContext *pvactx = s->priv_data;
  60. int syncword, streamid, reserved, flags, length, pts_flag;
  61. int64_t pva_pts = AV_NOPTS_VALUE, startpos;
  62. recover:
  63. startpos = avio_tell(pb);
  64. syncword = avio_rb16(pb);
  65. streamid = avio_r8(pb);
  66. avio_r8(pb); /* counter not used */
  67. reserved = avio_r8(pb);
  68. flags = avio_r8(pb);
  69. length = avio_rb16(pb);
  70. pts_flag = flags & 0x10;
  71. if (syncword != PVA_MAGIC) {
  72. pva_log(s, AV_LOG_ERROR, "invalid syncword\n");
  73. return AVERROR(EIO);
  74. }
  75. if (streamid != PVA_VIDEO_PAYLOAD && streamid != PVA_AUDIO_PAYLOAD) {
  76. pva_log(s, AV_LOG_ERROR, "invalid streamid\n");
  77. return AVERROR(EIO);
  78. }
  79. if (reserved != 0x55) {
  80. pva_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n");
  81. }
  82. if (length > PVA_MAX_PAYLOAD_LENGTH) {
  83. pva_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length);
  84. return AVERROR(EIO);
  85. }
  86. if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) {
  87. pva_pts = avio_rb32(pb);
  88. length -= 4;
  89. } else if (streamid == PVA_AUDIO_PAYLOAD) {
  90. /* PVA Audio Packets either start with a signaled PES packet or
  91. * are a continuation of the previous PES packet. New PES packets
  92. * always start at the beginning of a PVA Packet, never somewhere in
  93. * the middle. */
  94. if (!pvactx->continue_pes) {
  95. int pes_signal, pes_header_data_length, pes_packet_length,
  96. pes_flags;
  97. unsigned char pes_header_data[256];
  98. pes_signal = avio_rb24(pb);
  99. avio_r8(pb);
  100. pes_packet_length = avio_rb16(pb);
  101. pes_flags = avio_rb16(pb);
  102. pes_header_data_length = avio_r8(pb);
  103. if (pes_signal != 1) {
  104. pva_log(s, AV_LOG_WARNING, "expected signaled PES packet, "
  105. "trying to recover\n");
  106. avio_skip(pb, length - 9);
  107. if (!read_packet)
  108. return AVERROR(EIO);
  109. goto recover;
  110. }
  111. avio_read(pb, pes_header_data, pes_header_data_length);
  112. length -= 9 + pes_header_data_length;
  113. pes_packet_length -= 3 + pes_header_data_length;
  114. pvactx->continue_pes = pes_packet_length;
  115. if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20)
  116. pva_pts = ff_parse_pes_pts(pes_header_data);
  117. }
  118. pvactx->continue_pes -= length;
  119. if (pvactx->continue_pes < 0) {
  120. pva_log(s, AV_LOG_WARNING, "audio data corruption\n");
  121. pvactx->continue_pes = 0;
  122. }
  123. }
  124. if (pva_pts != AV_NOPTS_VALUE)
  125. av_add_index_entry(s->streams[streamid-1], startpos, pva_pts, 0, 0, AVINDEX_KEYFRAME);
  126. *pts = pva_pts;
  127. *len = length;
  128. *strid = streamid;
  129. return 0;
  130. }
  131. static int pva_read_packet(AVFormatContext *s, AVPacket *pkt) {
  132. AVIOContext *pb = s->pb;
  133. int64_t pva_pts;
  134. int ret, length, streamid;
  135. if (read_part_of_packet(s, &pva_pts, &length, &streamid, 1) < 0 ||
  136. (ret = av_get_packet(pb, pkt, length)) <= 0)
  137. return AVERROR(EIO);
  138. pkt->stream_index = streamid - 1;
  139. pkt->pts = pva_pts;
  140. return ret;
  141. }
  142. static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index,
  143. int64_t *pos, int64_t pos_limit) {
  144. AVIOContext *pb = s->pb;
  145. PVAContext *pvactx = s->priv_data;
  146. int length, streamid;
  147. int64_t res = AV_NOPTS_VALUE;
  148. pos_limit = FFMIN(*pos+PVA_MAX_PAYLOAD_LENGTH*8, (uint64_t)*pos+pos_limit);
  149. while (*pos < pos_limit) {
  150. res = AV_NOPTS_VALUE;
  151. avio_seek(pb, *pos, SEEK_SET);
  152. pvactx->continue_pes = 0;
  153. if (read_part_of_packet(s, &res, &length, &streamid, 0)) {
  154. (*pos)++;
  155. continue;
  156. }
  157. if (streamid - 1 != stream_index || res == AV_NOPTS_VALUE) {
  158. *pos = avio_tell(pb) + length;
  159. continue;
  160. }
  161. break;
  162. }
  163. pvactx->continue_pes = 0;
  164. return res;
  165. }
  166. AVInputFormat ff_pva_demuxer = {
  167. .name = "pva",
  168. .long_name = NULL_IF_CONFIG_SMALL("TechnoTrend PVA file and stream format"),
  169. .priv_data_size = sizeof(PVAContext),
  170. .read_probe = pva_probe,
  171. .read_header = pva_read_header,
  172. .read_packet = pva_read_packet,
  173. .read_timestamp = pva_read_timestamp
  174. };