| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (C) 2017 foo86 | ||
| 3 | * | ||
| 4 | * This file is part of FFmpeg. | ||
| 5 | * | ||
| 6 | * FFmpeg is free software; you can redistribute it and/or | ||
| 7 | * modify it under the terms of the GNU Lesser General Public | ||
| 8 | * License as published by the Free Software Foundation; either | ||
| 9 | * version 2.1 of the License, or (at your option) any later version. | ||
| 10 | * | ||
| 11 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * Lesser General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU Lesser General Public | ||
| 17 | * License along with FFmpeg; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include "libavutil/intreadwrite.h" | ||
| 22 | #include "avformat.h" | ||
| 23 | #include "demux.h" | ||
| 24 | #include "internal.h" | ||
| 25 | #include "spdif.h" | ||
| 26 | |||
| 27 | #define MARKER_16LE 0x72F81F4E | ||
| 28 | #define MARKER_20LE 0x20876FF0E154 | ||
| 29 | #define MARKER_24LE 0x72F8961F4EA5 | ||
| 30 | |||
| 31 | #define IS_16LE_MARKER(state) ((state & 0xFFFFFFFF) == MARKER_16LE) | ||
| 32 | #define IS_20LE_MARKER(state) ((state & 0xF0FFFFF0FFFF) == MARKER_20LE) | ||
| 33 | #define IS_24LE_MARKER(state) ((state & 0xFFFFFFFFFFFF) == MARKER_24LE) | ||
| 34 | #define IS_LE_MARKER(state) (IS_16LE_MARKER(state) || IS_20LE_MARKER(state) || IS_24LE_MARKER(state)) | ||
| 35 | |||
| 36 | 565 | static int s337m_get_offset_and_codec(void *avc, | |
| 37 | uint64_t state, | ||
| 38 | int data_type, int data_size, | ||
| 39 | int *offset, enum AVCodecID *codec) | ||
| 40 | { | ||
| 41 | int word_bits; | ||
| 42 | |||
| 43 |
1/2✓ Branch 0 taken 565 times.
✗ Branch 1 not taken.
|
565 | if (IS_16LE_MARKER(state)) { |
| 44 | 565 | word_bits = 16; | |
| 45 | ✗ | } else if (IS_20LE_MARKER(state)) { | |
| 46 | ✗ | data_type >>= 8; | |
| 47 | ✗ | data_size >>= 4; | |
| 48 | ✗ | word_bits = 20; | |
| 49 | } else { | ||
| 50 | ✗ | data_type >>= 8; | |
| 51 | ✗ | word_bits = 24; | |
| 52 | } | ||
| 53 | |||
| 54 |
2/2✓ Branch 0 taken 161 times.
✓ Branch 1 taken 404 times.
|
565 | if ((data_type & 0x1F) != 0x1C) { |
| 55 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 161 times.
|
161 | if (avc) |
| 56 | ✗ | avpriv_report_missing_feature(avc, "Data type %#x in SMPTE 337M", data_type & 0x1F); | |
| 57 | 161 | return AVERROR_PATCHWELCOME; | |
| 58 | } | ||
| 59 | |||
| 60 |
2/2✓ Branch 0 taken 384 times.
✓ Branch 1 taken 20 times.
|
404 | if (codec) |
| 61 | 384 | *codec = AV_CODEC_ID_DOLBY_E; | |
| 62 | |||
| 63 |
1/5✓ Branch 0 taken 404 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
404 | switch (data_size / word_bits) { |
| 64 | 404 | case 3648: | |
| 65 | 404 | *offset = 1920; | |
| 66 | 404 | break; | |
| 67 | ✗ | case 3644: | |
| 68 | ✗ | *offset = 2002; | |
| 69 | ✗ | break; | |
| 70 | ✗ | case 3640: | |
| 71 | ✗ | *offset = 2000; | |
| 72 | ✗ | break; | |
| 73 | ✗ | case 3040: | |
| 74 | ✗ | *offset = 1601; | |
| 75 | ✗ | break; | |
| 76 | ✗ | default: | |
| 77 | ✗ | if (avc) | |
| 78 | ✗ | avpriv_report_missing_feature(avc, "Dolby E data size %d in SMPTE 337M", data_size); | |
| 79 | ✗ | return AVERROR_PATCHWELCOME; | |
| 80 | } | ||
| 81 | |||
| 82 | 404 | *offset -= 4; | |
| 83 | 404 | *offset *= (word_bits + 7 >> 3) * 2; | |
| 84 | 404 | return 0; | |
| 85 | } | ||
| 86 | |||
| 87 | 7480 | static int s337m_probe(const AVProbeData *p) | |
| 88 | { | ||
| 89 | 7480 | uint64_t state = 0; | |
| 90 | 7480 | int markers[3] = { 0 }; | |
| 91 | int i, pos, sum, max, data_type, data_size, offset; | ||
| 92 | uint8_t *buf; | ||
| 93 | |||
| 94 |
2/2✓ Branch 0 taken 417491541 times.
✓ Branch 1 taken 7480 times.
|
417499021 | for (pos = 0; pos < p->buf_size; pos++) { |
| 95 | 417491541 | state = (state << 8) | p->buf[pos]; | |
| 96 |
4/6✓ Branch 0 taken 417491360 times.
✓ Branch 1 taken 181 times.
✓ Branch 2 taken 417491360 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 417491360 times.
✗ Branch 5 not taken.
|
417491541 | if (!IS_LE_MARKER(state)) |
| 97 | 417491360 | continue; | |
| 98 | |||
| 99 | 181 | buf = p->buf + pos + 1; | |
| 100 |
1/2✓ Branch 0 taken 181 times.
✗ Branch 1 not taken.
|
181 | if (IS_16LE_MARKER(state)) { |
| 101 | 181 | data_type = AV_RL16(buf ); | |
| 102 | 181 | data_size = AV_RL16(buf + 2); | |
| 103 | } else { | ||
| 104 | ✗ | data_type = AV_RL24(buf ); | |
| 105 | ✗ | data_size = AV_RL24(buf + 3); | |
| 106 | } | ||
| 107 | |||
| 108 |
2/2✓ Branch 1 taken 161 times.
✓ Branch 2 taken 20 times.
|
181 | if (s337m_get_offset_and_codec(NULL, state, data_type, data_size, &offset, NULL)) |
| 109 | 161 | continue; | |
| 110 | |||
| 111 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
20 | i = IS_16LE_MARKER(state) ? 0 : IS_20LE_MARKER(state) ? 1 : 2; |
| 112 | 20 | markers[i]++; | |
| 113 | |||
| 114 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | pos += IS_16LE_MARKER(state) ? 4 : 6; |
| 115 | 20 | pos += offset; | |
| 116 | 20 | state = 0; | |
| 117 | } | ||
| 118 | |||
| 119 | 7480 | sum = max = 0; | |
| 120 |
2/2✓ Branch 0 taken 22440 times.
✓ Branch 1 taken 7480 times.
|
29920 | for (i = 0; i < FF_ARRAY_ELEMS(markers); i++) { |
| 121 | 22440 | sum += markers[i]; | |
| 122 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22440 times.
|
22440 | if (markers[max] < markers[i]) |
| 123 | ✗ | max = i; | |
| 124 | } | ||
| 125 | |||
| 126 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7478 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
7480 | if (markers[max] > 3 && markers[max] * 4 > sum * 3) |
| 127 | 2 | return AVPROBE_SCORE_EXTENSION + 1; | |
| 128 | |||
| 129 | 7478 | return 0; | |
| 130 | } | ||
| 131 | |||
| 132 | 2 | static int s337m_read_header(AVFormatContext *s) | |
| 133 | { | ||
| 134 | 2 | s->ctx_flags |= AVFMTCTX_NOHEADER; | |
| 135 | 2 | return 0; | |
| 136 | } | ||
| 137 | |||
| 138 | ✗ | static void bswap_buf24(uint8_t *data, int size) | |
| 139 | { | ||
| 140 | int i; | ||
| 141 | |||
| 142 | ✗ | for (i = 0; i < size / 3; i++, data += 3) | |
| 143 | ✗ | FFSWAP(uint8_t, data[0], data[2]); | |
| 144 | ✗ | } | |
| 145 | |||
| 146 | 385 | static int s337m_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 147 | { | ||
| 148 | 385 | AVIOContext *pb = s->pb; | |
| 149 | 385 | uint64_t state = 0; | |
| 150 | int ret, data_type, data_size, offset; | ||
| 151 | enum AVCodecID codec; | ||
| 152 | |||
| 153 |
4/6✓ Branch 0 taken 7361 times.
✓ Branch 1 taken 384 times.
✓ Branch 2 taken 7361 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7361 times.
✗ Branch 5 not taken.
|
7745 | while (!IS_LE_MARKER(state)) { |
| 154 | 7361 | state = (state << 8) | avio_r8(pb); | |
| 155 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 7360 times.
|
7361 | if (avio_feof(pb)) |
| 156 | 1 | return AVERROR_EOF; | |
| 157 | } | ||
| 158 | |||
| 159 |
1/2✓ Branch 0 taken 384 times.
✗ Branch 1 not taken.
|
384 | if (IS_16LE_MARKER(state)) { |
| 160 | 384 | data_type = avio_rl16(pb); | |
| 161 | 384 | data_size = avio_rl16(pb); | |
| 162 | } else { | ||
| 163 | ✗ | data_type = avio_rl24(pb); | |
| 164 | ✗ | data_size = avio_rl24(pb); | |
| 165 | } | ||
| 166 | |||
| 167 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 384 times.
|
384 | if ((ret = s337m_get_offset_and_codec(s, state, data_type, data_size, &offset, &codec)) < 0) |
| 168 | ✗ | return ret; | |
| 169 | |||
| 170 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 384 times.
|
384 | if ((ret = av_get_packet(pb, pkt, offset)) != offset) |
| 171 | ✗ | return ret < 0 ? ret : AVERROR_EOF; | |
| 172 | |||
| 173 |
1/2✓ Branch 0 taken 384 times.
✗ Branch 1 not taken.
|
384 | if (IS_16LE_MARKER(state)) |
| 174 | 384 | ff_spdif_bswap_buf16((uint16_t *)pkt->data, (uint16_t *)pkt->data, pkt->size >> 1); | |
| 175 | else | ||
| 176 | ✗ | bswap_buf24(pkt->data, pkt->size); | |
| 177 | |||
| 178 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 382 times.
|
384 | if (!s->nb_streams) { |
| 179 | 2 | AVStream *st = avformat_new_stream(s, NULL); | |
| 180 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!st) { |
| 181 | ✗ | return AVERROR(ENOMEM); | |
| 182 | } | ||
| 183 | 2 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 184 | 2 | st->codecpar->codec_id = codec; | |
| 185 | 2 | ffstream(st)->need_parsing = AVSTREAM_PARSE_HEADERS; | |
| 186 | } | ||
| 187 | |||
| 188 | 384 | return 0; | |
| 189 | } | ||
| 190 | |||
| 191 | const FFInputFormat ff_s337m_demuxer = { | ||
| 192 | .p.name = "s337m", | ||
| 193 | .p.long_name = NULL_IF_CONFIG_SMALL("SMPTE 337M"), | ||
| 194 | .p.flags = AVFMT_GENERIC_INDEX, | ||
| 195 | .read_probe = s337m_probe, | ||
| 196 | .read_header = s337m_read_header, | ||
| 197 | .read_packet = s337m_read_packet, | ||
| 198 | }; | ||
| 199 |