2 * This file is part of FFmpeg.
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.
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.
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
25 int ff_alloc_a53_sei(const AVFrame *frame, size_t prefix_len,
26 void **data, size_t *sei_size)
28 AVFrameSideData *side_data = NULL;
32 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC);
39 *sei_size = side_data->size + 11;
40 *data = av_mallocz(*sei_size + prefix_len);
42 return AVERROR(ENOMEM);
43 sei_data = (uint8_t*)*data + prefix_len;
51 * 'GA94' is standard in North America for ATSC, but hard coding
52 * this style may not be the right thing to do -- other formats
53 * do exist. This information is not available in the side_data
54 * so we are going with this right now.
56 AV_WL32(sei_data + 3, MKTAG('G', 'A', '9', '4'));
58 sei_data[8] = ((side_data->size/3) & 0x1f) | 0x40;
61 memcpy(sei_data + 10, side_data->data, side_data->size);
63 sei_data[side_data->size+10] = 255;
68 int ff_parse_a53_cc(AVBufferRef **pbuf, const uint8_t *data, int size)
70 AVBufferRef *buf = *pbuf;
72 size_t new_size, old_size = buf ? buf->size : 0;
76 return AVERROR(EINVAL);
78 ret = init_get_bits8(&gb, data, size);
82 if (get_bits(&gb, 8) != 0x3) // user_data_type_code
85 skip_bits(&gb, 1); // reserved
86 if (!get_bits(&gb, 1)) // process_cc_data_flag
89 skip_bits(&gb, 1); // zero bit
90 cc_count = get_bits(&gb, 5);
94 skip_bits(&gb, 8); // reserved
96 /* 3 bytes per CC plus one byte marker_bits at the end */
97 if (cc_count * 3 >= (get_bits_left(&gb) >> 3))
98 return AVERROR(EINVAL);
100 new_size = (old_size + cc_count * 3);
102 if (new_size > INT_MAX)
103 return AVERROR(EINVAL);
105 /* Allow merging of the cc data from two fields. */
106 ret = av_buffer_realloc(pbuf, new_size);
111 /* Use of av_buffer_realloc assumes buffer is writeable */
112 for (int i = 0; i < cc_count; i++) {
113 buf->data[old_size++] = get_bits(&gb, 8);
114 buf->data[old_size++] = get_bits(&gb, 8);
115 buf->data[old_size++] = get_bits(&gb, 8);