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.

213 lines
6.7KB

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