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.

235 lines
7.4KB

  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(const 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. const 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_EXTENSION;
  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. int ret;
  75. recover:
  76. startpos = avio_tell(pb);
  77. syncword = avio_rb16(pb);
  78. streamid = avio_r8(pb);
  79. avio_r8(pb); /* counter not used */
  80. reserved = avio_r8(pb);
  81. flags = avio_r8(pb);
  82. length = avio_rb16(pb);
  83. pts_flag = flags & 0x10;
  84. if (syncword != PVA_MAGIC) {
  85. pva_log(s, AV_LOG_ERROR, "invalid syncword\n");
  86. return AVERROR(EIO);
  87. }
  88. if (streamid != PVA_VIDEO_PAYLOAD && streamid != PVA_AUDIO_PAYLOAD) {
  89. pva_log(s, AV_LOG_ERROR, "invalid streamid\n");
  90. return AVERROR(EIO);
  91. }
  92. if (reserved != 0x55) {
  93. pva_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n");
  94. }
  95. if (length > PVA_MAX_PAYLOAD_LENGTH) {
  96. pva_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length);
  97. return AVERROR(EIO);
  98. }
  99. if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) {
  100. pva_pts = avio_rb32(pb);
  101. length -= 4;
  102. } else if (streamid == PVA_AUDIO_PAYLOAD) {
  103. /* PVA Audio Packets either start with a signaled PES packet or
  104. * are a continuation of the previous PES packet. New PES packets
  105. * always start at the beginning of a PVA Packet, never somewhere in
  106. * the middle. */
  107. if (!pvactx->continue_pes) {
  108. int pes_signal, pes_header_data_length, pes_packet_length,
  109. pes_flags;
  110. unsigned char pes_header_data[256];
  111. pes_signal = avio_rb24(pb);
  112. avio_r8(pb);
  113. pes_packet_length = avio_rb16(pb);
  114. pes_flags = avio_rb16(pb);
  115. pes_header_data_length = avio_r8(pb);
  116. if (pes_signal != 1 || pes_header_data_length == 0) {
  117. pva_log(s, AV_LOG_WARNING, "expected non empty signaled PES packet, "
  118. "trying to recover\n");
  119. avio_skip(pb, length - 9);
  120. if (!read_packet)
  121. return AVERROR(EIO);
  122. goto recover;
  123. }
  124. ret = avio_read(pb, pes_header_data, pes_header_data_length);
  125. if (ret != pes_header_data_length)
  126. return ret < 0 ? ret : AVERROR_INVALIDDATA;
  127. length -= 9 + pes_header_data_length;
  128. pes_packet_length -= 3 + pes_header_data_length;
  129. pvactx->continue_pes = pes_packet_length;
  130. if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20) {
  131. if (pes_header_data_length < 5) {
  132. pva_log(s, AV_LOG_ERROR, "header too short\n");
  133. avio_skip(pb, length);
  134. return AVERROR_INVALIDDATA;
  135. }
  136. pva_pts = ff_parse_pes_pts(pes_header_data);
  137. }
  138. }
  139. pvactx->continue_pes -= length;
  140. if (pvactx->continue_pes < 0) {
  141. pva_log(s, AV_LOG_WARNING, "audio data corruption\n");
  142. pvactx->continue_pes = 0;
  143. }
  144. }
  145. if (pva_pts != AV_NOPTS_VALUE)
  146. av_add_index_entry(s->streams[streamid-1], startpos, pva_pts, 0, 0, AVINDEX_KEYFRAME);
  147. *pts = pva_pts;
  148. *len = length;
  149. *strid = streamid;
  150. return 0;
  151. }
  152. static int pva_read_packet(AVFormatContext *s, AVPacket *pkt) {
  153. AVIOContext *pb = s->pb;
  154. int64_t pva_pts;
  155. int ret, length, streamid;
  156. if (read_part_of_packet(s, &pva_pts, &length, &streamid, 1) < 0 ||
  157. (ret = av_get_packet(pb, pkt, length)) <= 0)
  158. return AVERROR(EIO);
  159. pkt->stream_index = streamid - 1;
  160. pkt->pts = pva_pts;
  161. return ret;
  162. }
  163. static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index,
  164. int64_t *pos, int64_t pos_limit) {
  165. AVIOContext *pb = s->pb;
  166. PVAContext *pvactx = s->priv_data;
  167. int length, streamid;
  168. int64_t res = AV_NOPTS_VALUE;
  169. pos_limit = FFMIN(*pos+PVA_MAX_PAYLOAD_LENGTH*8, (uint64_t)*pos+pos_limit);
  170. while (*pos < pos_limit) {
  171. res = AV_NOPTS_VALUE;
  172. avio_seek(pb, *pos, SEEK_SET);
  173. pvactx->continue_pes = 0;
  174. if (read_part_of_packet(s, &res, &length, &streamid, 0)) {
  175. (*pos)++;
  176. continue;
  177. }
  178. if (streamid - 1 != stream_index || res == AV_NOPTS_VALUE) {
  179. *pos = avio_tell(pb) + length;
  180. continue;
  181. }
  182. break;
  183. }
  184. pvactx->continue_pes = 0;
  185. return res;
  186. }
  187. AVInputFormat ff_pva_demuxer = {
  188. .name = "pva",
  189. .long_name = NULL_IF_CONFIG_SMALL("TechnoTrend PVA"),
  190. .priv_data_size = sizeof(PVAContext),
  191. .read_probe = pva_probe,
  192. .read_header = pva_read_header,
  193. .read_packet = pva_read_packet,
  194. .read_timestamp = pva_read_timestamp,
  195. };