pva.c 7.0 KB

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