pva.c 7.0 KB

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