FFmpeg
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/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 {
29  AVFrameSideData *side_data = NULL;
30  uint8_t *sei_data;
31 
32  if (frame)
34 
35  if (!side_data) {
36  *data = NULL;
37  return 0;
38  }
39 
40  *sei_size = side_data->size + 11;
41  *data = av_mallocz(*sei_size + prefix_len);
42  if (!*data)
43  return AVERROR(ENOMEM);
44  sei_data = (uint8_t*)*data + prefix_len;
45 
46  // country code
47  sei_data[0] = 181;
48  sei_data[1] = 0;
49  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  AV_WL32(sei_data + 3, MKTAG('G', 'A', '9', '4'));
58  sei_data[7] = 3;
59  sei_data[8] = ((side_data->size/3) & 0x1f) | 0x40;
60  sei_data[9] = 0;
61 
62  memcpy(sei_data + 10, side_data->data, side_data->size);
63 
64  sei_data[side_data->size+10] = 255;
65 
66  return 0;
67 }
68 
69 int ff_parse_a53_cc(AVBufferRef **pbuf, const uint8_t *data, int size)
70 {
71  AVBufferRef *buf = *pbuf;
72  GetBitContext gb;
73  size_t new_size, old_size = buf ? buf->size : 0;
74  int ret, cc_count;
75 
76  if (size < 3)
77  return AVERROR_INVALIDDATA;
78 
79  ret = init_get_bits8(&gb, data, size);
80  if (ret < 0)
81  return ret;
82 
83  if (get_bits(&gb, 8) != 0x3) // user_data_type_code
84  return 0;
85 
86  skip_bits(&gb, 1); // reserved
87  if (!get_bits(&gb, 1)) // process_cc_data_flag
88  return 0;
89 
90  skip_bits(&gb, 1); // zero bit
91  cc_count = get_bits(&gb, 5);
92  if (!cc_count)
93  return 0;
94 
95  skip_bits(&gb, 8); // reserved
96 
97  /* 3 bytes per CC plus one byte marker_bits at the end */
98  if (cc_count * 3 >= (get_bits_left(&gb) >> 3))
99  return AVERROR_INVALIDDATA;
100 
101  new_size = (old_size + cc_count * 3);
102 
103  if (new_size > INT_MAX)
104  return AVERROR_INVALIDDATA;
105 
106  /* Allow merging of the cc data from two fields. */
107  ret = av_buffer_realloc(pbuf, new_size);
108  if (ret < 0)
109  return ret;
110 
111  buf = *pbuf;
112  /* Use of av_buffer_realloc assumes buffer is writeable */
113  for (int i = 0; i < cc_count; i++) {
114  buf->data[old_size++] = get_bits(&gb, 8);
115  buf->data[old_size++] = get_bits(&gb, 8);
116  buf->data[old_size++] = get_bits(&gb, 8);
117  }
118 
119  return cc_count;
120 }
ff_alloc_a53_sei
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:26
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:695
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:422
av_frame_get_side_data
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition: frame.c:951
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
AV_FRAME_DATA_A53_CC
@ AV_FRAME_DATA_A53_CC
ATSC A53 Part 4 Closed Captions.
Definition: frame.h:59
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
data
const char data[16]
Definition: mxf.c:149
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:381
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
GetBitContext
Definition: get_bits.h:108
AVFrameSideData::size
size_t size
Definition: frame.h:268
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
get_bits.h
ff_parse_a53_cc
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:69
NULL
#define NULL
Definition: coverity.c:32
size
int size
Definition: twinvq_data.h:10344
AVFrameSideData::data
uint8_t * data
Definition: frame.h:267
AVBufferRef::size
size_t size
Size of data in bytes.
Definition: buffer.h:94
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
atsc_a53.h
av_buffer_realloc
int av_buffer_realloc(AVBufferRef **pbuf, size_t size)
Reallocate a given buffer.
Definition: buffer.c:183
mem.h
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
AVFrameSideData
Structure to hold side data for an AVFrame.
Definition: frame.h:265
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55