Coverage Report

Created: 2025-08-28 07:12

/src/ffmpeg/libavcodec/atsc_a53.c
Line
Count
Source (jump to first uncovered line)
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
#include <stddef.h>
20
#include <stdint.h>
21
22
#include "libavutil/mem.h"
23
#include "atsc_a53.h"
24
#include "get_bits.h"
25
26
int ff_alloc_a53_sei(const AVFrame *frame, size_t prefix_len,
27
                     void **data, size_t *sei_size)
28
0
{
29
0
    AVFrameSideData *side_data = NULL;
30
0
    uint8_t *sei_data;
31
32
0
    if (frame)
33
0
        side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC);
34
35
0
    if (!side_data) {
36
0
        *data = NULL;
37
0
        return 0;
38
0
    }
39
40
0
    *sei_size = side_data->size + 11;
41
0
    *data = av_mallocz(*sei_size + prefix_len);
42
0
    if (!*data)
43
0
        return AVERROR(ENOMEM);
44
0
    sei_data = (uint8_t*)*data + prefix_len;
45
46
    // country code
47
0
    sei_data[0] = 181;
48
0
    sei_data[1] = 0;
49
0
    sei_data[2] = 49;
50
51
    /**
52
     * 'GA94' is standard in North America for ATSC, but hard coding
53
     * this style may not be the right thing to do -- other formats
54
     * do exist. This information is not available in the side_data
55
     * so we are going with this right now.
56
     */
57
0
    AV_WL32(sei_data + 3, MKTAG('G', 'A', '9', '4'));
58
0
    sei_data[7] = 3;
59
0
    sei_data[8] = ((side_data->size/3) & 0x1f) | 0x40;
60
0
    sei_data[9] = 0;
61
62
0
    memcpy(sei_data + 10, side_data->data, side_data->size);
63
64
0
    sei_data[side_data->size+10] = 255;
65
66
0
    return 0;
67
0
}
68
69
int ff_parse_a53_cc(AVBufferRef **pbuf, const uint8_t *data, int size)
70
18.2k
{
71
18.2k
    AVBufferRef *buf = *pbuf;
72
18.2k
    GetBitContext gb;
73
18.2k
    size_t new_size, old_size = buf ? buf->size : 0;
74
18.2k
    int ret, cc_count;
75
76
18.2k
    if (size < 3)
77
1.88k
        return AVERROR_INVALIDDATA;
78
79
16.3k
    ret = init_get_bits8(&gb, data, size);
80
16.3k
    if (ret < 0)
81
0
        return ret;
82
83
16.3k
    if (get_bits(&gb, 8) != 0x3) // user_data_type_code
84
1.30k
        return 0;
85
86
15.0k
    skip_bits(&gb, 1); // reserved
87
15.0k
    if (!get_bits(&gb, 1)) // process_cc_data_flag
88
2.60k
        return 0;
89
90
12.4k
    skip_bits(&gb, 1); // zero bit
91
12.4k
    cc_count = get_bits(&gb, 5);
92
12.4k
    if (!cc_count)
93
1.87k
        return 0;
94
95
10.5k
    skip_bits(&gb, 8); // reserved
96
97
    /* 3 bytes per CC plus one byte marker_bits at the end */
98
10.5k
    if (cc_count * 3 >= (get_bits_left(&gb) >> 3))
99
3.16k
        return AVERROR_INVALIDDATA;
100
101
7.43k
    new_size = (old_size + cc_count * 3);
102
103
7.43k
    if (new_size > INT_MAX)
104
0
        return AVERROR_INVALIDDATA;
105
106
    /* Allow merging of the cc data from two fields. */
107
7.43k
    ret = av_buffer_realloc(pbuf, new_size);
108
7.43k
    if (ret < 0)
109
0
        return ret;
110
111
7.43k
    buf = *pbuf;
112
    /* Use of av_buffer_realloc assumes buffer is writeable */
113
78.1k
    for (int i = 0; i < cc_count; i++) {
114
70.6k
        buf->data[old_size++] = get_bits(&gb, 8);
115
70.6k
        buf->data[old_size++] = get_bits(&gb, 8);
116
70.6k
        buf->data[old_size++] = get_bits(&gb, 8);
117
70.6k
    }
118
119
7.43k
    return cc_count;
120
7.43k
}