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.

239 lines
7.5KB

  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 PVAContext {
  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 (avio_feof(pb)) {
  117. return AVERROR_EOF;
  118. }
  119. if (pes_signal != 1 || pes_header_data_length == 0) {
  120. pva_log(s, AV_LOG_WARNING, "expected non empty signaled PES packet, "
  121. "trying to recover\n");
  122. avio_skip(pb, length - 9);
  123. if (!read_packet)
  124. return AVERROR(EIO);
  125. goto recover;
  126. }
  127. ret = avio_read(pb, pes_header_data, pes_header_data_length);
  128. if (ret != pes_header_data_length)
  129. return ret < 0 ? ret : AVERROR_INVALIDDATA;
  130. length -= 9 + pes_header_data_length;
  131. pes_packet_length -= 3 + pes_header_data_length;
  132. pvactx->continue_pes = pes_packet_length;
  133. if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20) {
  134. if (pes_header_data_length < 5) {
  135. pva_log(s, AV_LOG_ERROR, "header too short\n");
  136. avio_skip(pb, length);
  137. return AVERROR_INVALIDDATA;
  138. }
  139. pva_pts = ff_parse_pes_pts(pes_header_data);
  140. }
  141. }
  142. pvactx->continue_pes -= length;
  143. if (pvactx->continue_pes < 0) {
  144. pva_log(s, AV_LOG_WARNING, "audio data corruption\n");
  145. pvactx->continue_pes = 0;
  146. }
  147. }
  148. if (pva_pts != AV_NOPTS_VALUE)
  149. av_add_index_entry(s->streams[streamid-1], startpos, pva_pts, 0, 0, AVINDEX_KEYFRAME);
  150. *pts = pva_pts;
  151. *len = length;
  152. *strid = streamid;
  153. return 0;
  154. }
  155. static int pva_read_packet(AVFormatContext *s, AVPacket *pkt) {
  156. AVIOContext *pb = s->pb;
  157. int64_t pva_pts;
  158. int ret, length, streamid;
  159. if (read_part_of_packet(s, &pva_pts, &length, &streamid, 1) < 0 ||
  160. (ret = av_get_packet(pb, pkt, length)) <= 0)
  161. return AVERROR(EIO);
  162. pkt->stream_index = streamid - 1;
  163. pkt->pts = pva_pts;
  164. return ret;
  165. }
  166. static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index,
  167. int64_t *pos, int64_t pos_limit) {
  168. AVIOContext *pb = s->pb;
  169. PVAContext *pvactx = s->priv_data;
  170. int length, streamid;
  171. int64_t res = AV_NOPTS_VALUE;
  172. pos_limit = FFMIN(*pos+PVA_MAX_PAYLOAD_LENGTH*8, (uint64_t)*pos+pos_limit);
  173. while (*pos < pos_limit) {
  174. res = AV_NOPTS_VALUE;
  175. avio_seek(pb, *pos, SEEK_SET);
  176. pvactx->continue_pes = 0;
  177. if (read_part_of_packet(s, &res, &length, &streamid, 0)) {
  178. (*pos)++;
  179. continue;
  180. }
  181. if (streamid - 1 != stream_index || res == AV_NOPTS_VALUE) {
  182. *pos = avio_tell(pb) + length;
  183. continue;
  184. }
  185. break;
  186. }
  187. pvactx->continue_pes = 0;
  188. return res;
  189. }
  190. AVInputFormat ff_pva_demuxer = {
  191. .name = "pva",
  192. .long_name = NULL_IF_CONFIG_SMALL("TechnoTrend PVA"),
  193. .priv_data_size = sizeof(PVAContext),
  194. .read_probe = pva_probe,
  195. .read_header = pva_read_header,
  196. .read_packet = pva_read_packet,
  197. .read_timestamp = pva_read_timestamp,
  198. };