Debian Codegraph @

gst-libs/gst/codecparsers/gstvp9bitwriter.c

raw symbols SCIP
1 /* GStreamer
2 * Copyright (C) 2022 Intel Corporation
3 * Author: He Junyan <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the0
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 /**
22 * SECTION:gstvp9bitwriter
23 * @title: GstVp9BitWriter
24 * @short_description: Functions for writing bitstream headers
25 *
26 * Since: 1.28
27 */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "gstvp9bitwriter.h"
34 #include <gst/base/gstbitwriter.h>
35
36 #ifndef GST_DISABLE_GST_DEBUG
37 #define GST_CAT_DEFAULT gst_vp9_debug_category_get()
38 static GstDebugCategory *
39 gst_vp9_debug_category_get (void)
40 {
41 static gsize cat_gonce = 0;
42
43 if (g_once_init_enter (&cat_gonce)) {
44 GstDebugCategory *cat = NULL;
45
46 GST_DEBUG_CATEGORY_INIT (cat, "bitwriter_vp9", 0, "vp9 bitwriter library");
47
48 g_once_init_leave (&cat_gonce, (gsize) cat);
49 }
50
51 return (GstDebugCategory *) cat_gonce;
52 }
53 #endif /* GST_DISABLE_GST_DEBUG */
54
55 #define WRITE_BITS_UNCHECK(bw, val, nbits) \
56 (nbits <= 8 ? gst_bit_writer_put_bits_uint8 (bw, val, nbits) : \
57 (nbits <= 16 ? gst_bit_writer_put_bits_uint16 (bw, val, nbits) : \
58 (nbits <= 32 ? gst_bit_writer_put_bits_uint32 (bw, val, nbits) : \
59 FALSE)))
60
61 #define WRITE_BITS(bw, val, nbits) \
62 if (!WRITE_BITS_UNCHECK (bw, val, nbits)) { \
63 g_warning ("Unsupported bit size: %u", nbits); \
64 have_space = FALSE; \
65 goto error; \
66 }
67
68 /**
69 * gst_vp9_bit_writer_frame_header:
70 * @frame_hdr: the frame header of #GstVp9FrameHdr to write
71 * @data: (out caller-allocates)(not nullable): the stream
72 * generated by the frame header
73 * @size: (inout): the size in bytes of the input and output
74 *
75 * Generating the according VP9 bit stream by providing the frame header.
76 *
77 * Returns: a #GstVp9BitWriterResult
78 *
79 * Since: 1.24
80 **/
81 GstVp9BitWriterResult
82 gst_vp9_bit_writer_frame_header (const GstVp9FrameHdr * frame_hdr,
83 guint8 * data, guint * size)
84 {
85 gboolean have_space = TRUE;
86 GstBitWriter bw;
87
88 g_return_val_if_fail (frame_hdr != NULL, GST_VP9_BIT_WRITER_ERROR);
89 g_return_val_if_fail (data != NULL, GST_VP9_BIT_WRITER_ERROR);
90 g_return_val_if_fail (size != NULL, GST_VP9_BIT_WRITER_ERROR);
91 g_return_val_if_fail (*size > 0, GST_VP9_BIT_WRITER_ERROR);
92
93 gst_bit_writer_init_with_data (&bw, data, *size, FALSE);
94
95 WRITE_BITS (&bw, GST_VP9_FRAME_MARKER, 2);
96 /* profile_low_bit */
97 WRITE_BITS (&bw, frame_hdr->profile & 0x01, 1);
98 /* profile_high_bit */
99 WRITE_BITS (&bw, (frame_hdr->profile & 0x02) >> 1, 1);
100 if (frame_hdr->profile == 3)
101 WRITE_BITS (&bw, 0, 1);
102
103 if (frame_hdr->show_existing_frame) {
104 WRITE_BITS (&bw, frame_hdr->show_existing_frame, 1);
105 WRITE_BITS (&bw, frame_hdr->frame_to_show, 3);
106 gst_bit_writer_align_bytes (&bw, 0);
107 } else {
108 GST_WARNING ("Frame header writing is not implemented.");
109 goto error;
110 }
111
112 g_assert (gst_bit_writer_get_size (&bw) % 8 == 0);
113 *size = gst_bit_writer_get_size (&bw) / 8;
114
115 gst_bit_writer_reset (&bw);
116
117 return GST_VP9_BIT_WRITER_OK;
118
119 error:
120 gst_bit_writer_reset (&bw);
121 *size = 0;
122 return have_space ? GST_VP9_BIT_WRITER_INVALID_DATA :
123 GST_VP9_BIT_WRITER_NO_MORE_SPACE;
124 }
125
126 /**
127 * gst_vp9_bit_writer_superframe_info:
128 * @frame_num: the frame number to composite this super frame
129 * @frame_size: the size of each frame
130 * @data: (inout): the stream data of this super frame
131 * @size: (inout): the size in bytes of the input and output
132 *
133 * Appending the super frame info at the end of this stream.
134 * Note: the input @data should already contain all frames' data in
135 * the same order of @frame_size.
136 *
137 * Returns: a #GstVp9BitWriterResult
138 *
139 * Since: 1.24
140 **/
141 GstVp9BitWriterResult
142 gst_vp9_bit_writer_superframe_info (guint frame_num, const gint * frame_size,
143 guint8 * data, guint * size)
144 {
145 GstBitWriter bw;
146 GstVp9BitWriterResult ret = GST_VP9_BIT_WRITER_OK;
147 guint data_sz = 0;
148 guint i, j;
149
150 g_return_val_if_fail (frame_num <= GST_VP9_MAX_FRAMES_IN_SUPERFRAME,
151 GST_VP9_BIT_WRITER_ERROR);
152 g_return_val_if_fail (frame_size != NULL, GST_VP9_BIT_WRITER_ERROR);
153 g_return_val_if_fail (data != NULL, GST_VP9_BIT_WRITER_ERROR);
154 g_return_val_if_fail (size != NULL, GST_VP9_BIT_WRITER_ERROR);
155
156 for (i = 0; i < frame_num; i++)
157 data_sz += frame_size[i];
158
159 if (*size < data_sz + 1 /* superframe_header */ +
160 4 * frame_num /* superframe_index */ + 1 /* superframe_header */ ) {
161 ret = GST_VP9_BIT_WRITER_NO_MORE_SPACE;
162 goto out;
163 }
164
165 memset (data + data_sz, 0, 1 + 4 * frame_num + 1);
166 gst_bit_writer_init_with_data (&bw, data + data_sz,
167 1 + 4 * frame_num + 1, FALSE);
168
169 /* superframe_header() */
170 gst_bit_writer_put_bits_uint8 (&bw, GST_VP9_SUPERFRAME_MARKER, 3);
171 /* bytes_per_framesize_minus_1 */
172 gst_bit_writer_put_bits_uint8 (&bw, 4 - 1, 2);
173 /* frames_in_superframe_minus_1 */
174 gst_bit_writer_put_bits_uint8 (&bw, frame_num - 1, 3);
175
176 for (i = 0; i < frame_num; i++) {
177 guint32 value = frame_size[i];
178 for (j = 0; j < 4; j++)
179 gst_bit_writer_put_bits_uint8 (&bw, (value >> j * 8) & 0xff, 8);
180 }
181
182 /* superframe_header() again */
183 gst_bit_writer_put_bits_uint8 (&bw, GST_VP9_SUPERFRAME_MARKER, 3);
184 /* bytes_per_framesize_minus_1 */
185 gst_bit_writer_put_bits_uint8 (&bw, 4 - 1, 2);
186 /* frames_in_superframe_minus_1 */
187 gst_bit_writer_put_bits_uint8 (&bw, frame_num - 1, 3);
188
189 g_assert (gst_bit_writer_get_size (&bw) % 8 == 0);
190 *size = data_sz + gst_bit_writer_get_size (&bw) / 8;
191
192 out:
193 gst_bit_writer_reset (&bw);
194
195 if (ret != GST_VP9_BIT_WRITER_OK)
196 *size = 0;
197
198 return ret;
199 }