| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of FFmpeg. | ||
| 3 | * | ||
| 4 | * FFmpeg is free software; you can redistribute it and/or | ||
| 5 | * modify it under the terms of the GNU Lesser General Public | ||
| 6 | * License as published by the Free Software Foundation; either | ||
| 7 | * version 2.1 of the License, or (at your option) any later version. | ||
| 8 | * | ||
| 9 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 12 | * Lesser General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU Lesser General Public | ||
| 15 | * License along with FFmpeg; if not, write to the Free Software | ||
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 17 | */ | ||
| 18 | |||
| 19 | /** | ||
| 20 | * @file | ||
| 21 | * This bitstream filter splits VP9 superframes into packets containing | ||
| 22 | * just one frame. | ||
| 23 | */ | ||
| 24 | |||
| 25 | #include <stddef.h> | ||
| 26 | |||
| 27 | #include "libavcodec/bsf.h" | ||
| 28 | #include "libavcodec/bsf_internal.h" | ||
| 29 | #include "libavcodec/bytestream.h" | ||
| 30 | #include "libavcodec/get_bits.h" | ||
| 31 | |||
| 32 | typedef struct VP9SFSplitContext { | ||
| 33 | AVPacket *buffer_pkt; | ||
| 34 | |||
| 35 | int nb_frames; | ||
| 36 | int next_frame; | ||
| 37 | size_t next_frame_offset; | ||
| 38 | int sizes[8]; | ||
| 39 | } VP9SFSplitContext; | ||
| 40 | |||
| 41 | 7130 | static int vp9_superframe_split_filter(AVBSFContext *ctx, AVPacket *out) | |
| 42 | { | ||
| 43 | 7130 | VP9SFSplitContext *s = ctx->priv_data; | |
| 44 | AVPacket *in; | ||
| 45 | int i, j, ret, marker; | ||
| 46 | 7130 | int is_superframe = !!s->buffer_pkt->data; | |
| 47 | |||
| 48 |
2/2✓ Branch 0 taken 7061 times.
✓ Branch 1 taken 69 times.
|
7130 | if (!s->buffer_pkt->data) { |
| 49 | 7061 | ret = ff_bsf_get_packet_ref(ctx, s->buffer_pkt); | |
| 50 |
2/2✓ Branch 0 taken 4767 times.
✓ Branch 1 taken 2294 times.
|
7061 | if (ret < 0) |
| 51 | 4767 | return ret; | |
| 52 | 2294 | in = s->buffer_pkt; | |
| 53 | |||
| 54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2294 times.
|
2294 | if (!in->size) |
| 55 | ✗ | goto passthrough; | |
| 56 | |||
| 57 | 2294 | marker = in->data[in->size - 1]; | |
| 58 |
2/2✓ Branch 0 taken 63 times.
✓ Branch 1 taken 2231 times.
|
2294 | if ((marker & 0xe0) == 0xc0) { |
| 59 | 63 | int length_size = 1 + ((marker >> 3) & 0x3); | |
| 60 | 63 | int nb_frames = 1 + (marker & 0x7); | |
| 61 | 63 | int idx_size = 2 + nb_frames * length_size; | |
| 62 | |||
| 63 |
2/4✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
|
63 | if (in->size >= idx_size && in->data[in->size - idx_size] == marker) { |
| 64 | GetByteContext bc; | ||
| 65 | 63 | int64_t total_size = 0; | |
| 66 | |||
| 67 | 63 | bytestream2_init(&bc, in->data + in->size + 1 - idx_size, | |
| 68 | nb_frames * length_size); | ||
| 69 | |||
| 70 |
2/2✓ Branch 0 taken 132 times.
✓ Branch 1 taken 63 times.
|
195 | for (i = 0; i < nb_frames; i++) { |
| 71 | 132 | int frame_size = 0; | |
| 72 |
2/2✓ Branch 0 taken 260 times.
✓ Branch 1 taken 132 times.
|
392 | for (j = 0; j < length_size; j++) |
| 73 | 260 | frame_size |= bytestream2_get_byte(&bc) << (j * 8); | |
| 74 | |||
| 75 | 132 | total_size += frame_size; | |
| 76 |
2/4✓ Branch 0 taken 132 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 132 times.
|
132 | if (frame_size <= 0 || total_size > in->size - idx_size) { |
| 77 | ✗ | av_log(ctx, AV_LOG_ERROR, | |
| 78 | "Invalid frame size in a superframe: %d\n", frame_size); | ||
| 79 | ✗ | ret = AVERROR(EINVAL); | |
| 80 | ✗ | goto fail; | |
| 81 | } | ||
| 82 | 132 | s->sizes[i] = frame_size; | |
| 83 | } | ||
| 84 | 63 | s->nb_frames = nb_frames; | |
| 85 | 63 | s->next_frame = 0; | |
| 86 | 63 | s->next_frame_offset = 0; | |
| 87 | 63 | is_superframe = 1; | |
| 88 | } | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 |
2/2✓ Branch 0 taken 132 times.
✓ Branch 1 taken 2231 times.
|
2363 | if (is_superframe) { |
| 93 | GetBitContext gb; | ||
| 94 | 132 | int profile, invisible = 0; | |
| 95 | |||
| 96 | 132 | ret = av_packet_ref(out, s->buffer_pkt); | |
| 97 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
|
132 | if (ret < 0) |
| 98 | ✗ | goto fail; | |
| 99 | |||
| 100 | 132 | out->data += s->next_frame_offset; | |
| 101 | 132 | out->size = s->sizes[s->next_frame]; | |
| 102 | |||
| 103 | 132 | s->next_frame_offset += out->size; | |
| 104 | 132 | s->next_frame++; | |
| 105 | |||
| 106 |
2/2✓ Branch 0 taken 63 times.
✓ Branch 1 taken 69 times.
|
132 | if (s->next_frame >= s->nb_frames) |
| 107 | 63 | av_packet_unref(s->buffer_pkt); | |
| 108 | |||
| 109 | 132 | ret = init_get_bits8(&gb, out->data, out->size); | |
| 110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
|
132 | if (ret < 0) |
| 111 | ✗ | goto fail; | |
| 112 | |||
| 113 | 132 | get_bits(&gb, 2); // frame_marker | |
| 114 | 132 | profile = get_bits1(&gb); | |
| 115 | 132 | profile |= get_bits1(&gb) << 1; | |
| 116 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 104 times.
|
132 | if (profile == 3) |
| 117 | 28 | get_bits1(&gb); | |
| 118 |
2/2✓ Branch 1 taken 128 times.
✓ Branch 2 taken 4 times.
|
132 | if (!get_bits1(&gb)) { |
| 119 | 128 | get_bits1(&gb); | |
| 120 | 128 | invisible = !get_bits1(&gb); | |
| 121 | } | ||
| 122 | |||
| 123 |
2/2✓ Branch 0 taken 69 times.
✓ Branch 1 taken 63 times.
|
132 | if (invisible) |
| 124 | 69 | out->pts = AV_NOPTS_VALUE; | |
| 125 | |||
| 126 | } else { | ||
| 127 | 2231 | passthrough: | |
| 128 | 2231 | av_packet_move_ref(out, s->buffer_pkt); | |
| 129 | } | ||
| 130 | |||
| 131 | 2363 | return 0; | |
| 132 | ✗ | fail: | |
| 133 | ✗ | if (ret < 0) | |
| 134 | ✗ | av_packet_unref(out); | |
| 135 | ✗ | av_packet_unref(s->buffer_pkt); | |
| 136 | ✗ | return ret; | |
| 137 | } | ||
| 138 | |||
| 139 | 487 | static int vp9_superframe_split_init(AVBSFContext *ctx) | |
| 140 | { | ||
| 141 | 487 | VP9SFSplitContext *s = ctx->priv_data; | |
| 142 | |||
| 143 | 487 | s->buffer_pkt = av_packet_alloc(); | |
| 144 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 487 times.
|
487 | if (!s->buffer_pkt) |
| 145 | ✗ | return AVERROR(ENOMEM); | |
| 146 | |||
| 147 | 487 | return 0; | |
| 148 | } | ||
| 149 | |||
| 150 | ✗ | static void vp9_superframe_split_flush(AVBSFContext *ctx) | |
| 151 | { | ||
| 152 | ✗ | VP9SFSplitContext *s = ctx->priv_data; | |
| 153 | ✗ | av_packet_unref(s->buffer_pkt); | |
| 154 | ✗ | } | |
| 155 | |||
| 156 | 487 | static void vp9_superframe_split_uninit(AVBSFContext *ctx) | |
| 157 | { | ||
| 158 | 487 | VP9SFSplitContext *s = ctx->priv_data; | |
| 159 | 487 | av_packet_free(&s->buffer_pkt); | |
| 160 | 487 | } | |
| 161 | |||
| 162 | const FFBitStreamFilter ff_vp9_superframe_split_bsf = { | ||
| 163 | .p.name = "vp9_superframe_split", | ||
| 164 | .p.codec_ids = (const enum AVCodecID []){ AV_CODEC_ID_VP9, AV_CODEC_ID_NONE }, | ||
| 165 | .priv_data_size = sizeof(VP9SFSplitContext), | ||
| 166 | .init = vp9_superframe_split_init, | ||
| 167 | .flush = vp9_superframe_split_flush, | ||
| 168 | .close = vp9_superframe_split_uninit, | ||
| 169 | .filter = vp9_superframe_split_filter, | ||
| 170 | }; | ||
| 171 |