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.

158 lines
4.9KB

  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. #define PVA_MAX_PAYLOAD_LENGTH 0x17f8
  23. #define PVA_VIDEO_PAYLOAD 0x01
  24. #define PVA_AUDIO_PAYLOAD 0x02
  25. #define PVA_MAGIC (('A' << 8) + 'V')
  26. typedef struct {
  27. int continue_pes;
  28. } PVAContext;
  29. static int pva_probe(AVProbeData * pd) {
  30. unsigned char *buf = pd->buf;
  31. if (AV_RB16(buf) == PVA_MAGIC && buf[2] && buf[2] < 3 && buf[4] == 0x55)
  32. return AVPROBE_SCORE_MAX / 2;
  33. return 0;
  34. }
  35. static int pva_read_header(AVFormatContext *s, AVFormatParameters *ap) {
  36. AVStream *st;
  37. if (!(st = av_new_stream(s, 0)))
  38. return AVERROR(ENOMEM);
  39. st->codec->codec_type = CODEC_TYPE_VIDEO;
  40. st->codec->codec_id = CODEC_ID_MPEG2VIDEO;
  41. st->need_parsing = AVSTREAM_PARSE_FULL;
  42. av_set_pts_info(st, 32, 1, 90000);
  43. if (!(st = av_new_stream(s, 1)))
  44. return AVERROR(ENOMEM);
  45. st->codec->codec_type = CODEC_TYPE_AUDIO;
  46. st->codec->codec_id = CODEC_ID_MP2;
  47. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  48. av_set_pts_info(st, 33, 1, 90000);
  49. /* the parameters will be extracted from the compressed bitstream */
  50. return 0;
  51. }
  52. static int pva_read_packet(AVFormatContext *s, AVPacket *pkt) {
  53. ByteIOContext *pb = s->pb;
  54. PVAContext *pvactx = s->priv_data;
  55. int ret, syncword, streamid, reserved, flags, length, pts_flag;
  56. int64_t pva_pts = AV_NOPTS_VALUE;
  57. syncword = get_be16(pb);
  58. streamid = get_byte(pb);
  59. get_byte(pb); /* counter not used */
  60. reserved = get_byte(pb);
  61. flags = get_byte(pb);
  62. length = get_be16(pb);
  63. pts_flag = flags & 0x10;
  64. if (syncword != PVA_MAGIC) {
  65. av_log(s, AV_LOG_ERROR, "invalid syncword\n");
  66. return AVERROR(EIO);
  67. }
  68. if (streamid != PVA_VIDEO_PAYLOAD && streamid != PVA_AUDIO_PAYLOAD) {
  69. av_log(s, AV_LOG_ERROR, "invalid streamid\n");
  70. return AVERROR(EIO);
  71. }
  72. if (reserved != 0x55) {
  73. av_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n");
  74. }
  75. if (length > PVA_MAX_PAYLOAD_LENGTH) {
  76. av_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length);
  77. return AVERROR(EIO);
  78. }
  79. if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) {
  80. pva_pts = get_be32(pb);
  81. length -= 4;
  82. } else if (streamid == PVA_AUDIO_PAYLOAD) {
  83. /* PVA Audio Packets either start with a signaled PES packet or
  84. * are a continuation of the previous PES packet. New PES packets
  85. * always start at the beginning of a PVA Packet, never somewhere in
  86. * the middle. */
  87. if (!pvactx->continue_pes) {
  88. int pes_signal, pes_header_data_length, pes_packet_length,
  89. pes_flags;
  90. unsigned char pes_header_data[256];
  91. pes_signal = get_be24(pb);
  92. get_byte(pb);
  93. pes_packet_length = get_be16(pb);
  94. pes_flags = get_be16(pb);
  95. pes_header_data_length = get_byte(pb);
  96. if (pes_signal != 1) {
  97. av_log(s, AV_LOG_ERROR, "expected signaled PES packet\n");
  98. return AVERROR(EIO);
  99. }
  100. get_buffer(pb, pes_header_data, pes_header_data_length);
  101. length -= 9 + pes_header_data_length;
  102. pes_packet_length -= 3 + pes_header_data_length;
  103. pvactx->continue_pes = pes_packet_length;
  104. if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20) {
  105. pva_pts = ((long long) *pes_header_data & 0x0e) << 29;
  106. pva_pts += (AV_RB16(pes_header_data+1) >> 1) << 15;
  107. pva_pts += AV_RB16(pes_header_data+3) >> 1;
  108. }
  109. }
  110. pvactx->continue_pes -= length;
  111. if (pvactx->continue_pes < 0) {
  112. av_log(s, AV_LOG_WARNING, "audio data corruption\n");
  113. pvactx->continue_pes = 0;
  114. }
  115. }
  116. if ((ret = av_get_packet(pb, pkt, length)) <= 0)
  117. return AVERROR(EIO);
  118. pkt->stream_index = streamid - 1;
  119. if (pva_pts)
  120. pkt->pts = pva_pts;
  121. return ret;
  122. }
  123. AVInputFormat pva_demuxer = {
  124. "pva",
  125. "pva file and stream format",
  126. sizeof(PVAContext),
  127. pva_probe,
  128. pva_read_header,
  129. pva_read_packet,
  130. };