| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * MLP parser | ||
| 3 | * Copyright (c) 2007 Ian Caulfield | ||
| 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 | /** | ||
| 23 | * @file | ||
| 24 | * MLP parser | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include <stdint.h> | ||
| 28 | |||
| 29 | #include "libavutil/internal.h" | ||
| 30 | #include "get_bits.h" | ||
| 31 | #include "parser.h" | ||
| 32 | #include "mlp_parse.h" | ||
| 33 | #include "mlp.h" | ||
| 34 | #include "parser_internal.h" | ||
| 35 | |||
| 36 | typedef struct MLPParseContext | ||
| 37 | { | ||
| 38 | ParseContext pc; | ||
| 39 | |||
| 40 | int bytes_left; | ||
| 41 | |||
| 42 | int in_sync; | ||
| 43 | |||
| 44 | int num_substreams; | ||
| 45 | } MLPParseContext; | ||
| 46 | |||
| 47 | 9 | static av_cold int mlp_init(AVCodecParserContext *s) | |
| 48 | { | ||
| 49 | 9 | ff_mlp_init_crc(); | |
| 50 | 9 | return 0; | |
| 51 | } | ||
| 52 | |||
| 53 | 33479 | static int mlp_parse(AVCodecParserContext *s, | |
| 54 | AVCodecContext *avctx, | ||
| 55 | const uint8_t **poutbuf, int *poutbuf_size, | ||
| 56 | const uint8_t *buf, int buf_size) | ||
| 57 | { | ||
| 58 | 33479 | MLPParseContext *mp = s->priv_data; | |
| 59 | int sync_present; | ||
| 60 | uint8_t parity_bits; | ||
| 61 | int next; | ||
| 62 | int ret; | ||
| 63 | 33479 | int i, p = 0; | |
| 64 | |||
| 65 | 33479 | s->key_frame = 0; | |
| 66 | |||
| 67 | 33479 | *poutbuf_size = 0; | |
| 68 | 33479 | *poutbuf = NULL; | |
| 69 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 33470 times.
|
33479 | if (buf_size == 0) |
| 70 | 9 | return 0; | |
| 71 | |||
| 72 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 33470 times.
|
33470 | if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) { |
| 73 | ✗ | next = buf_size; | |
| 74 | } else { | ||
| 75 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 33455 times.
|
33470 | if (!mp->in_sync) { |
| 76 | // Not in sync - find a major sync header | ||
| 77 | |||
| 78 |
2/2✓ Branch 0 taken 7146 times.
✓ Branch 1 taken 6 times.
|
7152 | for (i = 0; i < buf_size; i++) { |
| 79 | 7146 | mp->pc.state = (mp->pc.state << 8) | buf[i]; | |
| 80 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 7137 times.
|
7146 | if ((mp->pc.state & 0xfffffffe) == 0xf8726fba && |
| 81 | // ignore if we do not have the data for the start of header | ||
| 82 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 | mp->pc.index + i >= 7) { |
| 83 | 9 | mp->in_sync = 1; | |
| 84 | 9 | mp->bytes_left = 0; | |
| 85 | 9 | break; | |
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 9 times.
|
15 | if (!mp->in_sync) { |
| 90 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
|
6 | if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1) |
| 91 | ✗ | av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n"); | |
| 92 | 6 | return buf_size; | |
| 93 | } | ||
| 94 | |||
| 95 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
|
9 | if ((ret = ff_combine_frame(&mp->pc, i - 7, &buf, &buf_size)) < 0) { |
| 96 | ✗ | av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n"); | |
| 97 | ✗ | return ret; | |
| 98 | } | ||
| 99 | |||
| 100 | 9 | return i - 7; | |
| 101 | } | ||
| 102 | |||
| 103 |
2/2✓ Branch 0 taken 29359 times.
✓ Branch 1 taken 4096 times.
|
33455 | if (mp->bytes_left == 0) { |
| 104 | // Find length of this packet | ||
| 105 | |||
| 106 | /* Copy overread bytes from last frame into buffer. */ | ||
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29359 times.
|
29359 | for(; mp->pc.overread>0; mp->pc.overread--) { |
| 108 | ✗ | mp->pc.buffer[mp->pc.index++]= mp->pc.buffer[mp->pc.overread_index++]; | |
| 109 | } | ||
| 110 | |||
| 111 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29359 times.
|
29359 | if (mp->pc.index + buf_size < 2) { |
| 112 | ✗ | if (ff_combine_frame(&mp->pc, END_NOT_FOUND, &buf, &buf_size) != -1) | |
| 113 | ✗ | av_log(avctx, AV_LOG_WARNING, "ff_combine_frame failed\n"); | |
| 114 | ✗ | return buf_size; | |
| 115 | } | ||
| 116 | |||
| 117 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29359 times.
|
29359 | mp->bytes_left = ((mp->pc.index > 0 ? mp->pc.buffer[0] : buf[0]) << 8) |
| 118 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29359 times.
|
29359 | | (mp->pc.index > 1 ? mp->pc.buffer[1] : buf[1-mp->pc.index]); |
| 119 | 29359 | mp->bytes_left = (mp->bytes_left & 0xfff) * 2; | |
| 120 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29359 times.
|
29359 | if (mp->bytes_left <= 0) { // prevent infinite loop |
| 121 | ✗ | goto lost_sync; | |
| 122 | } | ||
| 123 | 29359 | mp->bytes_left -= mp->pc.index; | |
| 124 | } | ||
| 125 | |||
| 126 |
2/2✓ Branch 0 taken 29353 times.
✓ Branch 1 taken 4102 times.
|
33455 | next = (mp->bytes_left > buf_size) ? END_NOT_FOUND : mp->bytes_left; |
| 127 | |||
| 128 |
2/2✓ Branch 1 taken 4102 times.
✓ Branch 2 taken 29353 times.
|
33455 | if (ff_combine_frame(&mp->pc, next, &buf, &buf_size) < 0) { |
| 129 | 4102 | mp->bytes_left -= buf_size; | |
| 130 | 4102 | return buf_size; | |
| 131 | } | ||
| 132 | |||
| 133 | 29353 | mp->bytes_left = 0; | |
| 134 | } | ||
| 135 | |||
| 136 |
3/4✓ Branch 0 taken 29353 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2882 times.
✓ Branch 3 taken 26471 times.
|
29353 | sync_present = buf_size >= 8 && (AV_RB32(buf + 4) & 0xfffffffe) == 0xf8726fba; |
| 137 | |||
| 138 |
2/2✓ Branch 0 taken 26471 times.
✓ Branch 1 taken 2882 times.
|
29353 | if (!sync_present) { |
| 139 | /* The first nibble of a frame is a parity check of the 4-byte | ||
| 140 | * access unit header and all the 2- or 4-byte substream headers. */ | ||
| 141 | // Only check when this isn't a sync frame - syncs have a checksum. | ||
| 142 | |||
| 143 | 26471 | s->key_frame = 0; | |
| 144 | |||
| 145 | 26471 | parity_bits = 0; | |
| 146 |
2/2✓ Branch 0 taken 64471 times.
✓ Branch 1 taken 26471 times.
|
90942 | for (i = -1; i < mp->num_substreams; i++) { |
| 147 | 64471 | parity_bits ^= buf[p++]; | |
| 148 | 64471 | parity_bits ^= buf[p++]; | |
| 149 | |||
| 150 |
4/4✓ Branch 0 taken 38000 times.
✓ Branch 1 taken 26471 times.
✓ Branch 2 taken 168 times.
✓ Branch 3 taken 37832 times.
|
64471 | if (i < 0 || buf[p-2] & 0x80) { |
| 151 | 26639 | parity_bits ^= buf[p++]; | |
| 152 | 26639 | parity_bits ^= buf[p++]; | |
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26471 times.
|
26471 | if ((((parity_bits >> 4) ^ parity_bits) & 0xF) != 0xF) { |
| 157 | ✗ | av_log(avctx, AV_LOG_INFO, "mlpparse: Parity check failed.\n"); | |
| 158 | ✗ | goto lost_sync; | |
| 159 | } | ||
| 160 | } else { | ||
| 161 | GetBitContext gb; | ||
| 162 | MLPHeaderInfo mh; | ||
| 163 | |||
| 164 | 2882 | init_get_bits(&gb, buf + 4, (buf_size - 4) << 3); | |
| 165 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2882 times.
|
2882 | if (ff_mlp_read_major_sync(avctx, &mh, &gb) < 0) |
| 166 | ✗ | goto lost_sync; | |
| 167 | |||
| 168 | 2882 | s->key_frame = 1; | |
| 169 | |||
| 170 | 2882 | avctx->bits_per_raw_sample = mh.group1_bits; | |
| 171 |
2/2✓ Branch 0 taken 652 times.
✓ Branch 1 taken 2230 times.
|
2882 | if (avctx->bits_per_raw_sample > 16) |
| 172 | 652 | avctx->sample_fmt = AV_SAMPLE_FMT_S32; | |
| 173 | else | ||
| 174 | 2230 | avctx->sample_fmt = AV_SAMPLE_FMT_S16; | |
| 175 | 2882 | avctx->sample_rate = mh.group1_samplerate; | |
| 176 | 2882 | avctx->frame_size = | |
| 177 | 2882 | s->duration = mh.access_unit_size; | |
| 178 | |||
| 179 | 2882 | av_channel_layout_uninit(&avctx->ch_layout); | |
| 180 |
2/2✓ Branch 0 taken 2230 times.
✓ Branch 1 taken 652 times.
|
2882 | if (mh.stream_type == 0xbb) { |
| 181 | /* MLP stream */ | ||
| 182 | 2230 | av_channel_layout_from_mask(&avctx->ch_layout, mh.channel_layout_mlp); | |
| 183 | } else { /* mh.stream_type == 0xba */ | ||
| 184 | /* TrueHD stream */ | ||
| 185 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 652 times.
|
652 | if (!mh.channels_thd_stream2) { |
| 186 | ✗ | av_channel_layout_from_mask(&avctx->ch_layout, mh.channel_layout_thd_stream1); | |
| 187 | } else { | ||
| 188 | 652 | av_channel_layout_from_mask(&avctx->ch_layout, mh.channel_layout_thd_stream2); | |
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2882 times.
|
2882 | if (!mh.is_vbr) /* Stream is CBR */ |
| 193 | ✗ | avctx->bit_rate = mh.peak_bitrate; | |
| 194 | |||
| 195 | 2882 | mp->num_substreams = mh.num_substreams; | |
| 196 | } | ||
| 197 | |||
| 198 | 29353 | *poutbuf = buf; | |
| 199 | 29353 | *poutbuf_size = buf_size; | |
| 200 | |||
| 201 | 29353 | return next; | |
| 202 | |||
| 203 | ✗ | lost_sync: | |
| 204 | ✗ | mp->in_sync = 0; | |
| 205 | ✗ | return 1; | |
| 206 | } | ||
| 207 | |||
| 208 | const FFCodecParser ff_mlp_parser = { | ||
| 209 | PARSER_CODEC_LIST(AV_CODEC_ID_MLP, AV_CODEC_ID_TRUEHD), | ||
| 210 | .priv_data_size = sizeof(MLPParseContext), | ||
| 211 | .init = mlp_init, | ||
| 212 | .parse = mlp_parse, | ||
| 213 | .close = ff_parse_close, | ||
| 214 | }; | ||
| 215 |