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.

154 lines
4.8KB

  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_MP3;
  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. long long pva_pts = 0;
  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) >> 4;
  64. if (syncword != PVA_MAGIC) {
  65. av_log(s, AV_LOG_ERROR, "invalid syncword\n");
  66. return AVERROR(EIO);
  67. }
  68. if (reserved != 0x55) {
  69. av_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n");
  70. }
  71. if (length > PVA_MAX_PAYLOAD_LENGTH) {
  72. av_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length);
  73. return AVERROR(EIO);
  74. }
  75. if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) {
  76. pva_pts = get_be32(pb);
  77. length -= 4;
  78. } else if (streamid == PVA_AUDIO_PAYLOAD) {
  79. /* PVA Audio Packets either start with a signaled PES packet or
  80. * are a continuation of the previous PES packet. New PES packets
  81. * always start at the beginning of a PVA Packet, never somewhere in
  82. * the middle. */
  83. if (!pvactx->continue_pes) {
  84. int pes_signal, pes_header_data_length, pes_packet_length,
  85. pes_flags;
  86. unsigned char pes_header_data[256];
  87. pes_signal = get_be24(pb);
  88. get_byte(pb);
  89. pes_packet_length = get_be16(pb);
  90. pes_flags = get_be16(pb);
  91. pes_header_data_length = get_byte(pb);
  92. if (pes_signal != 1) {
  93. av_log(s, AV_LOG_ERROR, "expected signaled PES packet\n");
  94. return AVERROR(EIO);
  95. }
  96. get_buffer(pb, pes_header_data, pes_header_data_length);
  97. length -= 9 + pes_header_data_length;
  98. pes_packet_length -= 3 + pes_header_data_length;
  99. pvactx->continue_pes = pes_packet_length;
  100. if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20) {
  101. pva_pts = ((long long) *pes_header_data & 0x0e) << 29;
  102. pva_pts += (AV_RB16(pes_header_data+1) >> 1) << 15;
  103. pva_pts += AV_RB16(pes_header_data+3) >> 1;
  104. }
  105. }
  106. pvactx->continue_pes -= length;
  107. if (pvactx->continue_pes < 0) {
  108. av_log(s, AV_LOG_WARNING, "audio data corruption\n");
  109. pvactx->continue_pes = 0;
  110. }
  111. }
  112. if ((ret = av_get_packet(pb, pkt, length)) <= 0)
  113. return AVERROR(EIO);
  114. pkt->stream_index = streamid - 1;
  115. if (pva_pts)
  116. pkt->pts = pva_pts;
  117. return ret;
  118. }
  119. AVInputFormat pva_demuxer = {
  120. "pva",
  121. "pva file and stream format",
  122. sizeof(PVAContext),
  123. pva_probe,
  124. pva_read_header,
  125. pva_read_packet,
  126. };