FFmpeg  4.4.9
atsc_a53.c
Go to the documentation of this file.
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/intreadwrite.h"
23 #include "atsc_a53.h"
24 
25 int ff_alloc_a53_sei(const AVFrame *frame, size_t prefix_len,
26  void **data, size_t *sei_size)
27 {
28  AVFrameSideData *side_data = NULL;
29  uint8_t *sei_data;
30 
31  if (frame)
33 
34  if (!side_data) {
35  *data = NULL;
36  return 0;
37  }
38 
39  *sei_size = side_data->size + 11;
40  *data = av_mallocz(*sei_size + prefix_len);
41  if (!*data)
42  return AVERROR(ENOMEM);
43  sei_data = (uint8_t*)*data + prefix_len;
44 
45  // country code
46  sei_data[0] = 181;
47  sei_data[1] = 0;
48  sei_data[2] = 49;
49 
50  /**
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.
55  */
56  AV_WL32(sei_data + 3, MKTAG('G', 'A', '9', '4'));
57  sei_data[7] = 3;
58  sei_data[8] = ((side_data->size/3) & 0x1f) | 0x40;
59  sei_data[9] = 0;
60 
61  memcpy(sei_data + 10, side_data->data, side_data->size);
62 
63  sei_data[side_data->size+10] = 255;
64 
65  return 0;
66 }
67 
68 int ff_parse_a53_cc(AVBufferRef **pbuf, const uint8_t *data, int size)
69 {
70  AVBufferRef *buf = *pbuf;
71  size_t new_size, old_size = buf ? buf->size : 0;
72  int ret, cc_count;
73 
74  if (size < 3)
75  return AVERROR(EINVAL);
76 
77  if (data[0] != 0x3) // user_data_type_code
78  return 0;
79 
80  if (!(data[1] & 0x40)) // process_cc_data_flag
81  return 0;
82 
83  cc_count = data[1] & 0x1F;
84  if (!cc_count)
85  return 0;
86 
87  // content of data[2] is reserved
88  /* 3 bytes per CC plus one byte marker_bits at the end */
89  if (cc_count * 3 >= size - 3)
90  return AVERROR(EINVAL);
91 
92  new_size = (old_size + cc_count * 3);
93 
94  if (new_size > INT_MAX)
95  return AVERROR(EINVAL);
96 
97  /* Allow merging of the cc data from two fields. */
98  ret = av_buffer_realloc(pbuf, new_size);
99  if (ret < 0)
100  return ret;
101 
102  buf = *pbuf;
103  /* Use of av_buffer_realloc assumes buffer is writeable */
104  memcpy(buf->data + old_size, data + 3, cc_count * 3);
105 
106  return cc_count;
107 }
int ff_parse_a53_cc(AVBufferRef **pbuf, const uint8_t *data, int size)
Parse a data array for ATSC A53 Part 4 Closed Captions and store them in an AVBufferRef.
Definition: atsc_a53.c:68
int ff_alloc_a53_sei(const AVFrame *frame, size_t prefix_len, void **data, size_t *sei_size)
Check AVFrame for A53 side data and allocate and fill SEI message with A53 info.
Definition: atsc_a53.c:25
uint8_t
#define MKTAG(a, b, c, d)
Definition: common.h:478
#define NULL
Definition: coverity.c:32
static AVFrame * frame
int av_buffer_realloc(AVBufferRef **pbuf, buffer_size_t size)
Reallocate a given buffer.
Definition: buffer.c:169
#define AVERROR(e)
Definition: error.h:43
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:738
@ AV_FRAME_DATA_A53_CC
ATSC A53 Part 4 Closed Captions.
Definition: frame.h:58
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
const char data[16]
Definition: mxf.c:142
A reference to a data buffer.
Definition: buffer.h:84
int size
Size of data in bytes.
Definition: buffer.h:97
uint8_t * data
The data buffer.
Definition: buffer.h:92
Structure to hold side data for an AVFrame.
Definition: frame.h:220
uint8_t * data
Definition: frame.h:222
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int size