| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 22 | #include "avformat.h" | ||
| 23 | #include "avio_internal.h" | ||
| 24 | #include "demux.h" | ||
| 25 | #include "internal.h" | ||
| 26 | #include "mpeg.h" | ||
| 27 | |||
| 28 | #define PVA_MAX_PAYLOAD_LENGTH 0x17f8 | ||
| 29 | #define PVA_VIDEO_PAYLOAD 0x01 | ||
| 30 | #define PVA_AUDIO_PAYLOAD 0x02 | ||
| 31 | #define PVA_MAGIC (('A' << 8) + 'V') | ||
| 32 | |||
| 33 | typedef struct PVAContext { | ||
| 34 | int continue_pes; | ||
| 35 | } PVAContext; | ||
| 36 | |||
| 37 | 7475 | static int pva_check(const uint8_t *p) { | |
| 38 | 7475 | int length = AV_RB16(p + 6); | |
| 39 |
6/8✓ Branch 0 taken 5 times.
✓ Branch 1 taken 7470 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
|
7475 | if (AV_RB16(p) != PVA_MAGIC || !p[2] || p[2] > 2 || p[4] != 0x55 || |
| 40 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | (p[5] & 0xe0) || length > PVA_MAX_PAYLOAD_LENGTH) |
| 41 | 7471 | return -1; | |
| 42 | 4 | return length + 8; | |
| 43 | } | ||
| 44 | |||
| 45 | 7474 | static int pva_probe(const AVProbeData * pd) { | |
| 46 | 7474 | const unsigned char *buf = pd->buf; | |
| 47 | 7474 | int len = pva_check(buf); | |
| 48 | |||
| 49 |
2/2✓ Branch 0 taken 7471 times.
✓ Branch 1 taken 3 times.
|
7474 | if (len < 0) |
| 50 | 7471 | return 0; | |
| 51 | |||
| 52 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
4 | if (pd->buf_size >= len + 8 && |
| 53 | 1 | pva_check(buf + len) >= 0) | |
| 54 | 1 | return AVPROBE_SCORE_EXTENSION; | |
| 55 | |||
| 56 | 2 | return AVPROBE_SCORE_MAX / 4; | |
| 57 | } | ||
| 58 | |||
| 59 | 1 | static int pva_read_header(AVFormatContext *s) { | |
| 60 | AVStream *st; | ||
| 61 | |||
| 62 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (!(st = avformat_new_stream(s, NULL))) |
| 63 | ✗ | return AVERROR(ENOMEM); | |
| 64 | 1 | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
| 65 | 1 | st->codecpar->codec_id = AV_CODEC_ID_MPEG2VIDEO; | |
| 66 | 1 | ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL; | |
| 67 | 1 | avpriv_set_pts_info(st, 32, 1, 90000); | |
| 68 | 1 | av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME); | |
| 69 | |||
| 70 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (!(st = avformat_new_stream(s, NULL))) |
| 71 | ✗ | return AVERROR(ENOMEM); | |
| 72 | 1 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 73 | 1 | st->codecpar->codec_id = AV_CODEC_ID_MP2; | |
| 74 | 1 | ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL; | |
| 75 | 1 | avpriv_set_pts_info(st, 33, 1, 90000); | |
| 76 | 1 | av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME); | |
| 77 | |||
| 78 | /* the parameters will be extracted from the compressed bitstream */ | ||
| 79 | 1 | return 0; | |
| 80 | } | ||
| 81 | |||
| 82 | #define pva_log if (read_packet) av_log | ||
| 83 | |||
| 84 | 115 | static int read_part_of_packet(AVFormatContext *s, int64_t *pts, | |
| 85 | int *len, int *strid, int read_packet) { | ||
| 86 | 115 | AVIOContext *pb = s->pb; | |
| 87 | 115 | PVAContext *pvactx = s->priv_data; | |
| 88 | int syncword, streamid, reserved, flags, length, pts_flag; | ||
| 89 | 115 | int64_t pva_pts = AV_NOPTS_VALUE, startpos; | |
| 90 | int ret; | ||
| 91 | |||
| 92 | 115 | recover: | |
| 93 | 115 | startpos = avio_tell(pb); | |
| 94 | |||
| 95 | 115 | syncword = avio_rb16(pb); | |
| 96 | 115 | streamid = avio_r8(pb); | |
| 97 | 115 | avio_r8(pb); /* counter not used */ | |
| 98 | 115 | reserved = avio_r8(pb); | |
| 99 | 115 | flags = avio_r8(pb); | |
| 100 | 115 | length = avio_rb16(pb); | |
| 101 | |||
| 102 | 115 | pts_flag = flags & 0x10; | |
| 103 | |||
| 104 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
|
115 | if (syncword != PVA_MAGIC) { |
| 105 | ✗ | pva_log(s, AV_LOG_ERROR, "invalid syncword\n"); | |
| 106 | ✗ | return AVERROR_INVALIDDATA; | |
| 107 | } | ||
| 108 |
3/4✓ Branch 0 taken 11 times.
✓ Branch 1 taken 104 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
|
115 | if (streamid != PVA_VIDEO_PAYLOAD && streamid != PVA_AUDIO_PAYLOAD) { |
| 109 | ✗ | pva_log(s, AV_LOG_ERROR, "invalid streamid\n"); | |
| 110 | ✗ | return AVERROR_INVALIDDATA; | |
| 111 | } | ||
| 112 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
|
115 | if (reserved != 0x55) { |
| 113 | ✗ | pva_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n"); | |
| 114 | } | ||
| 115 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 115 times.
|
115 | if (length > PVA_MAX_PAYLOAD_LENGTH) { |
| 116 | ✗ | pva_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length); | |
| 117 | ✗ | return AVERROR_INVALIDDATA; | |
| 118 | } | ||
| 119 | |||
| 120 |
4/4✓ Branch 0 taken 104 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 74 times.
|
115 | if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) { |
| 121 | 30 | pva_pts = avio_rb32(pb); | |
| 122 | 30 | length -= 4; | |
| 123 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 74 times.
|
85 | } else if (streamid == PVA_AUDIO_PAYLOAD) { |
| 124 | /* PVA Audio Packets either start with a signaled PES packet or | ||
| 125 | * are a continuation of the previous PES packet. New PES packets | ||
| 126 | * always start at the beginning of a PVA Packet, never somewhere in | ||
| 127 | * the middle. */ | ||
| 128 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | if (!pvactx->continue_pes) { |
| 129 | int pes_signal, pes_header_data_length, pes_packet_length, | ||
| 130 | pes_flags; | ||
| 131 | unsigned char pes_header_data[256]; | ||
| 132 | |||
| 133 | 11 | pes_signal = avio_rb24(pb); | |
| 134 | 11 | avio_r8(pb); | |
| 135 | 11 | pes_packet_length = avio_rb16(pb); | |
| 136 | 11 | pes_flags = avio_rb16(pb); | |
| 137 | 11 | pes_header_data_length = avio_r8(pb); | |
| 138 | |||
| 139 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
|
11 | if (avio_feof(pb)) { |
| 140 | ✗ | return AVERROR_EOF; | |
| 141 | } | ||
| 142 | |||
| 143 |
2/4✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
|
11 | if (pes_signal != 1 || pes_header_data_length == 0) { |
| 144 | ✗ | pva_log(s, AV_LOG_WARNING, "expected non empty signaled PES packet, " | |
| 145 | "trying to recover\n"); | ||
| 146 | ✗ | avio_skip(pb, length - 9); | |
| 147 | ✗ | if (!read_packet) | |
| 148 | ✗ | return AVERROR_INVALIDDATA; | |
| 149 | ✗ | goto recover; | |
| 150 | } | ||
| 151 | |||
| 152 | 11 | ret = ffio_read_size(pb, pes_header_data, pes_header_data_length); | |
| 153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (ret < 0) |
| 154 | ✗ | return ret; | |
| 155 | 11 | length -= 9 + pes_header_data_length; | |
| 156 | |||
| 157 | 11 | pes_packet_length -= 3 + pes_header_data_length; | |
| 158 | |||
| 159 | 11 | pvactx->continue_pes = pes_packet_length; | |
| 160 | |||
| 161 |
2/4✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
|
11 | if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20) { |
| 162 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (pes_header_data_length < 5) { |
| 163 | ✗ | pva_log(s, AV_LOG_ERROR, "header too short\n"); | |
| 164 | ✗ | avio_skip(pb, length); | |
| 165 | ✗ | return AVERROR_INVALIDDATA; | |
| 166 | } | ||
| 167 | 11 | pva_pts = ff_parse_pes_pts(pes_header_data); | |
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | 11 | pvactx->continue_pes -= length; | |
| 172 | |||
| 173 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (pvactx->continue_pes < 0) { |
| 174 | ✗ | pva_log(s, AV_LOG_WARNING, "audio data corruption\n"); | |
| 175 | ✗ | pvactx->continue_pes = 0; | |
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 |
2/2✓ Branch 0 taken 41 times.
✓ Branch 1 taken 74 times.
|
115 | if (pva_pts != AV_NOPTS_VALUE) |
| 180 | 41 | av_add_index_entry(s->streams[streamid-1], startpos, pva_pts, 0, 0, AVINDEX_KEYFRAME); | |
| 181 | |||
| 182 | 115 | *pts = pva_pts; | |
| 183 | 115 | *len = length; | |
| 184 | 115 | *strid = streamid; | |
| 185 | 115 | return 0; | |
| 186 | } | ||
| 187 | |||
| 188 | 115 | static int pva_read_packet(AVFormatContext *s, AVPacket *pkt) { | |
| 189 | 115 | AVIOContext *pb = s->pb; | |
| 190 | int64_t pva_pts; | ||
| 191 | int ret, length, streamid; | ||
| 192 | |||
| 193 |
2/4✓ Branch 1 taken 115 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 115 times.
|
230 | if (read_part_of_packet(s, &pva_pts, &length, &streamid, 1) < 0 || |
| 194 | 115 | (ret = av_get_packet(pb, pkt, length)) <= 0) | |
| 195 | ✗ | return AVERROR_INVALIDDATA; | |
| 196 | |||
| 197 | 115 | pkt->stream_index = streamid - 1; | |
| 198 | 115 | pkt->pts = pva_pts; | |
| 199 | |||
| 200 | 115 | return ret; | |
| 201 | } | ||
| 202 | |||
| 203 | ✗ | static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index, | |
| 204 | int64_t *pos, int64_t pos_limit) { | ||
| 205 | ✗ | AVIOContext *pb = s->pb; | |
| 206 | ✗ | PVAContext *pvactx = s->priv_data; | |
| 207 | int length, streamid; | ||
| 208 | ✗ | int64_t res = AV_NOPTS_VALUE; | |
| 209 | |||
| 210 | ✗ | pos_limit = FFMIN(*pos+PVA_MAX_PAYLOAD_LENGTH*8, (uint64_t)*pos+pos_limit); | |
| 211 | |||
| 212 | ✗ | while (*pos < pos_limit) { | |
| 213 | ✗ | res = AV_NOPTS_VALUE; | |
| 214 | ✗ | avio_seek(pb, *pos, SEEK_SET); | |
| 215 | |||
| 216 | ✗ | pvactx->continue_pes = 0; | |
| 217 | ✗ | if (read_part_of_packet(s, &res, &length, &streamid, 0)) { | |
| 218 | ✗ | (*pos)++; | |
| 219 | ✗ | continue; | |
| 220 | } | ||
| 221 | ✗ | if (streamid - 1 != stream_index || res == AV_NOPTS_VALUE) { | |
| 222 | ✗ | *pos = avio_tell(pb) + length; | |
| 223 | ✗ | continue; | |
| 224 | } | ||
| 225 | ✗ | break; | |
| 226 | } | ||
| 227 | |||
| 228 | ✗ | pvactx->continue_pes = 0; | |
| 229 | ✗ | return res; | |
| 230 | } | ||
| 231 | |||
| 232 | const FFInputFormat ff_pva_demuxer = { | ||
| 233 | .p.name = "pva", | ||
| 234 | .p.long_name = NULL_IF_CONFIG_SMALL("TechnoTrend PVA"), | ||
| 235 | .priv_data_size = sizeof(PVAContext), | ||
| 236 | .read_probe = pva_probe, | ||
| 237 | .read_header = pva_read_header, | ||
| 238 | .read_packet = pva_read_packet, | ||
| 239 | .read_timestamp = pva_read_timestamp, | ||
| 240 | }; | ||
| 241 |