1*02a8b425SSebastian Fricke // SPDX-License-Identifier: GPL-2.0-or-later
2*02a8b425SSebastian Fricke /*
3*02a8b425SSebastian Fricke * Coda multi-standard codec IP - H.264 helper functions
4*02a8b425SSebastian Fricke *
5*02a8b425SSebastian Fricke * Copyright (C) 2012 Vista Silicon S.L.
6*02a8b425SSebastian Fricke * Javier Martin, <javier.martin@vista-silicon.com>
7*02a8b425SSebastian Fricke * Xavier Duret
8*02a8b425SSebastian Fricke */
9*02a8b425SSebastian Fricke
10*02a8b425SSebastian Fricke #include <linux/kernel.h>
11*02a8b425SSebastian Fricke #include <linux/string.h>
12*02a8b425SSebastian Fricke #include <linux/videodev2.h>
13*02a8b425SSebastian Fricke
14*02a8b425SSebastian Fricke #include "coda.h"
15*02a8b425SSebastian Fricke
16*02a8b425SSebastian Fricke static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
17*02a8b425SSebastian Fricke
coda_find_nal_header(const u8 * buf,const u8 * end)18*02a8b425SSebastian Fricke static const u8 *coda_find_nal_header(const u8 *buf, const u8 *end)
19*02a8b425SSebastian Fricke {
20*02a8b425SSebastian Fricke u32 val = 0xffffffff;
21*02a8b425SSebastian Fricke
22*02a8b425SSebastian Fricke do {
23*02a8b425SSebastian Fricke val = val << 8 | *buf++;
24*02a8b425SSebastian Fricke if (buf >= end)
25*02a8b425SSebastian Fricke return NULL;
26*02a8b425SSebastian Fricke } while (val != 0x00000001);
27*02a8b425SSebastian Fricke
28*02a8b425SSebastian Fricke return buf;
29*02a8b425SSebastian Fricke }
30*02a8b425SSebastian Fricke
coda_sps_parse_profile(struct coda_ctx * ctx,struct vb2_buffer * vb)31*02a8b425SSebastian Fricke int coda_sps_parse_profile(struct coda_ctx *ctx, struct vb2_buffer *vb)
32*02a8b425SSebastian Fricke {
33*02a8b425SSebastian Fricke const u8 *buf = vb2_plane_vaddr(vb, 0);
34*02a8b425SSebastian Fricke const u8 *end = buf + vb2_get_plane_payload(vb, 0);
35*02a8b425SSebastian Fricke
36*02a8b425SSebastian Fricke /* Find SPS header */
37*02a8b425SSebastian Fricke do {
38*02a8b425SSebastian Fricke buf = coda_find_nal_header(buf, end);
39*02a8b425SSebastian Fricke if (!buf)
40*02a8b425SSebastian Fricke return -EINVAL;
41*02a8b425SSebastian Fricke } while ((*buf++ & 0x1f) != 0x7);
42*02a8b425SSebastian Fricke
43*02a8b425SSebastian Fricke ctx->params.h264_profile_idc = buf[0];
44*02a8b425SSebastian Fricke ctx->params.h264_level_idc = buf[2];
45*02a8b425SSebastian Fricke
46*02a8b425SSebastian Fricke return 0;
47*02a8b425SSebastian Fricke }
48*02a8b425SSebastian Fricke
coda_h264_filler_nal(int size,char * p)49*02a8b425SSebastian Fricke int coda_h264_filler_nal(int size, char *p)
50*02a8b425SSebastian Fricke {
51*02a8b425SSebastian Fricke if (size < 6)
52*02a8b425SSebastian Fricke return -EINVAL;
53*02a8b425SSebastian Fricke
54*02a8b425SSebastian Fricke p[0] = 0x00;
55*02a8b425SSebastian Fricke p[1] = 0x00;
56*02a8b425SSebastian Fricke p[2] = 0x00;
57*02a8b425SSebastian Fricke p[3] = 0x01;
58*02a8b425SSebastian Fricke p[4] = 0x0c;
59*02a8b425SSebastian Fricke memset(p + 5, 0xff, size - 6);
60*02a8b425SSebastian Fricke /* Add rbsp stop bit and trailing at the end */
61*02a8b425SSebastian Fricke p[size - 1] = 0x80;
62*02a8b425SSebastian Fricke
63*02a8b425SSebastian Fricke return 0;
64*02a8b425SSebastian Fricke }
65*02a8b425SSebastian Fricke
coda_h264_padding(int size,char * p)66*02a8b425SSebastian Fricke int coda_h264_padding(int size, char *p)
67*02a8b425SSebastian Fricke {
68*02a8b425SSebastian Fricke int nal_size;
69*02a8b425SSebastian Fricke int diff;
70*02a8b425SSebastian Fricke
71*02a8b425SSebastian Fricke diff = size - (size & ~0x7);
72*02a8b425SSebastian Fricke if (diff == 0)
73*02a8b425SSebastian Fricke return 0;
74*02a8b425SSebastian Fricke
75*02a8b425SSebastian Fricke nal_size = coda_filler_size[diff];
76*02a8b425SSebastian Fricke coda_h264_filler_nal(nal_size, p);
77*02a8b425SSebastian Fricke
78*02a8b425SSebastian Fricke return nal_size;
79*02a8b425SSebastian Fricke }
80*02a8b425SSebastian Fricke
coda_h264_profile(int profile_idc)81*02a8b425SSebastian Fricke int coda_h264_profile(int profile_idc)
82*02a8b425SSebastian Fricke {
83*02a8b425SSebastian Fricke switch (profile_idc) {
84*02a8b425SSebastian Fricke case 66: return V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE;
85*02a8b425SSebastian Fricke case 77: return V4L2_MPEG_VIDEO_H264_PROFILE_MAIN;
86*02a8b425SSebastian Fricke case 88: return V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED;
87*02a8b425SSebastian Fricke case 100: return V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
88*02a8b425SSebastian Fricke default: return -EINVAL;
89*02a8b425SSebastian Fricke }
90*02a8b425SSebastian Fricke }
91*02a8b425SSebastian Fricke
coda_h264_level(int level_idc)92*02a8b425SSebastian Fricke int coda_h264_level(int level_idc)
93*02a8b425SSebastian Fricke {
94*02a8b425SSebastian Fricke switch (level_idc) {
95*02a8b425SSebastian Fricke case 10: return V4L2_MPEG_VIDEO_H264_LEVEL_1_0;
96*02a8b425SSebastian Fricke case 9: return V4L2_MPEG_VIDEO_H264_LEVEL_1B;
97*02a8b425SSebastian Fricke case 11: return V4L2_MPEG_VIDEO_H264_LEVEL_1_1;
98*02a8b425SSebastian Fricke case 12: return V4L2_MPEG_VIDEO_H264_LEVEL_1_2;
99*02a8b425SSebastian Fricke case 13: return V4L2_MPEG_VIDEO_H264_LEVEL_1_3;
100*02a8b425SSebastian Fricke case 20: return V4L2_MPEG_VIDEO_H264_LEVEL_2_0;
101*02a8b425SSebastian Fricke case 21: return V4L2_MPEG_VIDEO_H264_LEVEL_2_1;
102*02a8b425SSebastian Fricke case 22: return V4L2_MPEG_VIDEO_H264_LEVEL_2_2;
103*02a8b425SSebastian Fricke case 30: return V4L2_MPEG_VIDEO_H264_LEVEL_3_0;
104*02a8b425SSebastian Fricke case 31: return V4L2_MPEG_VIDEO_H264_LEVEL_3_1;
105*02a8b425SSebastian Fricke case 32: return V4L2_MPEG_VIDEO_H264_LEVEL_3_2;
106*02a8b425SSebastian Fricke case 40: return V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
107*02a8b425SSebastian Fricke case 41: return V4L2_MPEG_VIDEO_H264_LEVEL_4_1;
108*02a8b425SSebastian Fricke case 42: return V4L2_MPEG_VIDEO_H264_LEVEL_4_2;
109*02a8b425SSebastian Fricke case 50: return V4L2_MPEG_VIDEO_H264_LEVEL_5_0;
110*02a8b425SSebastian Fricke case 51: return V4L2_MPEG_VIDEO_H264_LEVEL_5_1;
111*02a8b425SSebastian Fricke default: return -EINVAL;
112*02a8b425SSebastian Fricke }
113*02a8b425SSebastian Fricke }
114*02a8b425SSebastian Fricke
115*02a8b425SSebastian Fricke struct rbsp {
116*02a8b425SSebastian Fricke char *buf;
117*02a8b425SSebastian Fricke int size;
118*02a8b425SSebastian Fricke int pos;
119*02a8b425SSebastian Fricke };
120*02a8b425SSebastian Fricke
rbsp_read_bit(struct rbsp * rbsp)121*02a8b425SSebastian Fricke static inline int rbsp_read_bit(struct rbsp *rbsp)
122*02a8b425SSebastian Fricke {
123*02a8b425SSebastian Fricke int shift = 7 - (rbsp->pos % 8);
124*02a8b425SSebastian Fricke int ofs = rbsp->pos++ / 8;
125*02a8b425SSebastian Fricke
126*02a8b425SSebastian Fricke if (ofs >= rbsp->size)
127*02a8b425SSebastian Fricke return -EINVAL;
128*02a8b425SSebastian Fricke
129*02a8b425SSebastian Fricke return (rbsp->buf[ofs] >> shift) & 1;
130*02a8b425SSebastian Fricke }
131*02a8b425SSebastian Fricke
rbsp_write_bit(struct rbsp * rbsp,int bit)132*02a8b425SSebastian Fricke static inline int rbsp_write_bit(struct rbsp *rbsp, int bit)
133*02a8b425SSebastian Fricke {
134*02a8b425SSebastian Fricke int shift = 7 - (rbsp->pos % 8);
135*02a8b425SSebastian Fricke int ofs = rbsp->pos++ / 8;
136*02a8b425SSebastian Fricke
137*02a8b425SSebastian Fricke if (ofs >= rbsp->size)
138*02a8b425SSebastian Fricke return -EINVAL;
139*02a8b425SSebastian Fricke
140*02a8b425SSebastian Fricke rbsp->buf[ofs] &= ~(1 << shift);
141*02a8b425SSebastian Fricke rbsp->buf[ofs] |= bit << shift;
142*02a8b425SSebastian Fricke
143*02a8b425SSebastian Fricke return 0;
144*02a8b425SSebastian Fricke }
145*02a8b425SSebastian Fricke
rbsp_read_bits(struct rbsp * rbsp,int num,int * val)146*02a8b425SSebastian Fricke static inline int rbsp_read_bits(struct rbsp *rbsp, int num, int *val)
147*02a8b425SSebastian Fricke {
148*02a8b425SSebastian Fricke int i, ret;
149*02a8b425SSebastian Fricke int tmp = 0;
150*02a8b425SSebastian Fricke
151*02a8b425SSebastian Fricke if (num > 32)
152*02a8b425SSebastian Fricke return -EINVAL;
153*02a8b425SSebastian Fricke
154*02a8b425SSebastian Fricke for (i = 0; i < num; i++) {
155*02a8b425SSebastian Fricke ret = rbsp_read_bit(rbsp);
156*02a8b425SSebastian Fricke if (ret < 0)
157*02a8b425SSebastian Fricke return ret;
158*02a8b425SSebastian Fricke tmp |= ret << (num - i - 1);
159*02a8b425SSebastian Fricke }
160*02a8b425SSebastian Fricke
161*02a8b425SSebastian Fricke if (val)
162*02a8b425SSebastian Fricke *val = tmp;
163*02a8b425SSebastian Fricke
164*02a8b425SSebastian Fricke return 0;
165*02a8b425SSebastian Fricke }
166*02a8b425SSebastian Fricke
rbsp_write_bits(struct rbsp * rbsp,int num,int value)167*02a8b425SSebastian Fricke static int rbsp_write_bits(struct rbsp *rbsp, int num, int value)
168*02a8b425SSebastian Fricke {
169*02a8b425SSebastian Fricke int ret;
170*02a8b425SSebastian Fricke
171*02a8b425SSebastian Fricke while (num--) {
172*02a8b425SSebastian Fricke ret = rbsp_write_bit(rbsp, (value >> num) & 1);
173*02a8b425SSebastian Fricke if (ret)
174*02a8b425SSebastian Fricke return ret;
175*02a8b425SSebastian Fricke }
176*02a8b425SSebastian Fricke
177*02a8b425SSebastian Fricke return 0;
178*02a8b425SSebastian Fricke }
179*02a8b425SSebastian Fricke
rbsp_read_uev(struct rbsp * rbsp,unsigned int * val)180*02a8b425SSebastian Fricke static int rbsp_read_uev(struct rbsp *rbsp, unsigned int *val)
181*02a8b425SSebastian Fricke {
182*02a8b425SSebastian Fricke int leading_zero_bits = 0;
183*02a8b425SSebastian Fricke unsigned int tmp = 0;
184*02a8b425SSebastian Fricke int ret;
185*02a8b425SSebastian Fricke
186*02a8b425SSebastian Fricke while ((ret = rbsp_read_bit(rbsp)) == 0)
187*02a8b425SSebastian Fricke leading_zero_bits++;
188*02a8b425SSebastian Fricke if (ret < 0)
189*02a8b425SSebastian Fricke return ret;
190*02a8b425SSebastian Fricke
191*02a8b425SSebastian Fricke if (leading_zero_bits > 0) {
192*02a8b425SSebastian Fricke ret = rbsp_read_bits(rbsp, leading_zero_bits, &tmp);
193*02a8b425SSebastian Fricke if (ret)
194*02a8b425SSebastian Fricke return ret;
195*02a8b425SSebastian Fricke }
196*02a8b425SSebastian Fricke
197*02a8b425SSebastian Fricke if (val)
198*02a8b425SSebastian Fricke *val = (1 << leading_zero_bits) - 1 + tmp;
199*02a8b425SSebastian Fricke
200*02a8b425SSebastian Fricke return 0;
201*02a8b425SSebastian Fricke }
202*02a8b425SSebastian Fricke
rbsp_write_uev(struct rbsp * rbsp,unsigned int value)203*02a8b425SSebastian Fricke static int rbsp_write_uev(struct rbsp *rbsp, unsigned int value)
204*02a8b425SSebastian Fricke {
205*02a8b425SSebastian Fricke int i;
206*02a8b425SSebastian Fricke int ret;
207*02a8b425SSebastian Fricke int tmp = value + 1;
208*02a8b425SSebastian Fricke int leading_zero_bits = fls(tmp) - 1;
209*02a8b425SSebastian Fricke
210*02a8b425SSebastian Fricke for (i = 0; i < leading_zero_bits; i++) {
211*02a8b425SSebastian Fricke ret = rbsp_write_bit(rbsp, 0);
212*02a8b425SSebastian Fricke if (ret)
213*02a8b425SSebastian Fricke return ret;
214*02a8b425SSebastian Fricke }
215*02a8b425SSebastian Fricke
216*02a8b425SSebastian Fricke return rbsp_write_bits(rbsp, leading_zero_bits + 1, tmp);
217*02a8b425SSebastian Fricke }
218*02a8b425SSebastian Fricke
rbsp_read_sev(struct rbsp * rbsp,int * val)219*02a8b425SSebastian Fricke static int rbsp_read_sev(struct rbsp *rbsp, int *val)
220*02a8b425SSebastian Fricke {
221*02a8b425SSebastian Fricke unsigned int tmp;
222*02a8b425SSebastian Fricke int ret;
223*02a8b425SSebastian Fricke
224*02a8b425SSebastian Fricke ret = rbsp_read_uev(rbsp, &tmp);
225*02a8b425SSebastian Fricke if (ret)
226*02a8b425SSebastian Fricke return ret;
227*02a8b425SSebastian Fricke
228*02a8b425SSebastian Fricke if (val) {
229*02a8b425SSebastian Fricke if (tmp & 1)
230*02a8b425SSebastian Fricke *val = (tmp + 1) / 2;
231*02a8b425SSebastian Fricke else
232*02a8b425SSebastian Fricke *val = -(tmp / 2);
233*02a8b425SSebastian Fricke }
234*02a8b425SSebastian Fricke
235*02a8b425SSebastian Fricke return 0;
236*02a8b425SSebastian Fricke }
237*02a8b425SSebastian Fricke
238*02a8b425SSebastian Fricke /**
239*02a8b425SSebastian Fricke * coda_h264_sps_fixup - fixes frame cropping values in h.264 SPS
240*02a8b425SSebastian Fricke * @ctx: encoder context
241*02a8b425SSebastian Fricke * @width: visible width
242*02a8b425SSebastian Fricke * @height: visible height
243*02a8b425SSebastian Fricke * @buf: buffer containing h.264 SPS RBSP, starting with NAL header
244*02a8b425SSebastian Fricke * @size: modified RBSP size return value
245*02a8b425SSebastian Fricke * @max_size: available size in buf
246*02a8b425SSebastian Fricke *
247*02a8b425SSebastian Fricke * Rewrites the frame cropping values in an h.264 SPS RBSP correctly for the
248*02a8b425SSebastian Fricke * given visible width and height.
249*02a8b425SSebastian Fricke */
coda_h264_sps_fixup(struct coda_ctx * ctx,int width,int height,char * buf,int * size,int max_size)250*02a8b425SSebastian Fricke int coda_h264_sps_fixup(struct coda_ctx *ctx, int width, int height, char *buf,
251*02a8b425SSebastian Fricke int *size, int max_size)
252*02a8b425SSebastian Fricke {
253*02a8b425SSebastian Fricke int profile_idc;
254*02a8b425SSebastian Fricke unsigned int pic_order_cnt_type;
255*02a8b425SSebastian Fricke int pic_width_in_mbs_minus1, pic_height_in_map_units_minus1;
256*02a8b425SSebastian Fricke int frame_mbs_only_flag, frame_cropping_flag;
257*02a8b425SSebastian Fricke int vui_parameters_present_flag;
258*02a8b425SSebastian Fricke unsigned int crop_right, crop_bottom;
259*02a8b425SSebastian Fricke struct rbsp sps;
260*02a8b425SSebastian Fricke int pos;
261*02a8b425SSebastian Fricke int ret;
262*02a8b425SSebastian Fricke
263*02a8b425SSebastian Fricke if (*size < 8 || *size >= max_size)
264*02a8b425SSebastian Fricke return -EINVAL;
265*02a8b425SSebastian Fricke
266*02a8b425SSebastian Fricke sps.buf = buf + 5; /* Skip NAL header */
267*02a8b425SSebastian Fricke sps.size = *size - 5;
268*02a8b425SSebastian Fricke
269*02a8b425SSebastian Fricke profile_idc = sps.buf[0];
270*02a8b425SSebastian Fricke /* Skip constraint_set[0-5]_flag, reserved_zero_2bits */
271*02a8b425SSebastian Fricke /* Skip level_idc */
272*02a8b425SSebastian Fricke sps.pos = 24;
273*02a8b425SSebastian Fricke
274*02a8b425SSebastian Fricke /* seq_parameter_set_id */
275*02a8b425SSebastian Fricke ret = rbsp_read_uev(&sps, NULL);
276*02a8b425SSebastian Fricke if (ret)
277*02a8b425SSebastian Fricke return ret;
278*02a8b425SSebastian Fricke
279*02a8b425SSebastian Fricke if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 ||
280*02a8b425SSebastian Fricke profile_idc == 244 || profile_idc == 44 || profile_idc == 83 ||
281*02a8b425SSebastian Fricke profile_idc == 86 || profile_idc == 118 || profile_idc == 128 ||
282*02a8b425SSebastian Fricke profile_idc == 138 || profile_idc == 139 || profile_idc == 134 ||
283*02a8b425SSebastian Fricke profile_idc == 135) {
284*02a8b425SSebastian Fricke dev_err(ctx->fh.vdev->dev_parent,
285*02a8b425SSebastian Fricke "%s: Handling profile_idc %d not implemented\n",
286*02a8b425SSebastian Fricke __func__, profile_idc);
287*02a8b425SSebastian Fricke return -EINVAL;
288*02a8b425SSebastian Fricke }
289*02a8b425SSebastian Fricke
290*02a8b425SSebastian Fricke /* log2_max_frame_num_minus4 */
291*02a8b425SSebastian Fricke ret = rbsp_read_uev(&sps, NULL);
292*02a8b425SSebastian Fricke if (ret)
293*02a8b425SSebastian Fricke return ret;
294*02a8b425SSebastian Fricke
295*02a8b425SSebastian Fricke ret = rbsp_read_uev(&sps, &pic_order_cnt_type);
296*02a8b425SSebastian Fricke if (ret)
297*02a8b425SSebastian Fricke return ret;
298*02a8b425SSebastian Fricke
299*02a8b425SSebastian Fricke if (pic_order_cnt_type == 0) {
300*02a8b425SSebastian Fricke /* log2_max_pic_order_cnt_lsb_minus4 */
301*02a8b425SSebastian Fricke ret = rbsp_read_uev(&sps, NULL);
302*02a8b425SSebastian Fricke if (ret)
303*02a8b425SSebastian Fricke return ret;
304*02a8b425SSebastian Fricke } else if (pic_order_cnt_type == 1) {
305*02a8b425SSebastian Fricke unsigned int i, num_ref_frames_in_pic_order_cnt_cycle;
306*02a8b425SSebastian Fricke
307*02a8b425SSebastian Fricke /* delta_pic_order_always_zero_flag */
308*02a8b425SSebastian Fricke ret = rbsp_read_bit(&sps);
309*02a8b425SSebastian Fricke if (ret < 0)
310*02a8b425SSebastian Fricke return ret;
311*02a8b425SSebastian Fricke /* offset_for_non_ref_pic */
312*02a8b425SSebastian Fricke ret = rbsp_read_sev(&sps, NULL);
313*02a8b425SSebastian Fricke if (ret)
314*02a8b425SSebastian Fricke return ret;
315*02a8b425SSebastian Fricke /* offset_for_top_to_bottom_field */
316*02a8b425SSebastian Fricke ret = rbsp_read_sev(&sps, NULL);
317*02a8b425SSebastian Fricke if (ret)
318*02a8b425SSebastian Fricke return ret;
319*02a8b425SSebastian Fricke
320*02a8b425SSebastian Fricke ret = rbsp_read_uev(&sps,
321*02a8b425SSebastian Fricke &num_ref_frames_in_pic_order_cnt_cycle);
322*02a8b425SSebastian Fricke if (ret)
323*02a8b425SSebastian Fricke return ret;
324*02a8b425SSebastian Fricke for (i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++) {
325*02a8b425SSebastian Fricke /* offset_for_ref_frame */
326*02a8b425SSebastian Fricke ret = rbsp_read_sev(&sps, NULL);
327*02a8b425SSebastian Fricke if (ret)
328*02a8b425SSebastian Fricke return ret;
329*02a8b425SSebastian Fricke }
330*02a8b425SSebastian Fricke }
331*02a8b425SSebastian Fricke
332*02a8b425SSebastian Fricke /* max_num_ref_frames */
333*02a8b425SSebastian Fricke ret = rbsp_read_uev(&sps, NULL);
334*02a8b425SSebastian Fricke if (ret)
335*02a8b425SSebastian Fricke return ret;
336*02a8b425SSebastian Fricke
337*02a8b425SSebastian Fricke /* gaps_in_frame_num_value_allowed_flag */
338*02a8b425SSebastian Fricke ret = rbsp_read_bit(&sps);
339*02a8b425SSebastian Fricke if (ret < 0)
340*02a8b425SSebastian Fricke return ret;
341*02a8b425SSebastian Fricke ret = rbsp_read_uev(&sps, &pic_width_in_mbs_minus1);
342*02a8b425SSebastian Fricke if (ret)
343*02a8b425SSebastian Fricke return ret;
344*02a8b425SSebastian Fricke ret = rbsp_read_uev(&sps, &pic_height_in_map_units_minus1);
345*02a8b425SSebastian Fricke if (ret)
346*02a8b425SSebastian Fricke return ret;
347*02a8b425SSebastian Fricke frame_mbs_only_flag = ret = rbsp_read_bit(&sps);
348*02a8b425SSebastian Fricke if (ret < 0)
349*02a8b425SSebastian Fricke return ret;
350*02a8b425SSebastian Fricke if (!frame_mbs_only_flag) {
351*02a8b425SSebastian Fricke /* mb_adaptive_frame_field_flag */
352*02a8b425SSebastian Fricke ret = rbsp_read_bit(&sps);
353*02a8b425SSebastian Fricke if (ret < 0)
354*02a8b425SSebastian Fricke return ret;
355*02a8b425SSebastian Fricke }
356*02a8b425SSebastian Fricke /* direct_8x8_inference_flag */
357*02a8b425SSebastian Fricke ret = rbsp_read_bit(&sps);
358*02a8b425SSebastian Fricke if (ret < 0)
359*02a8b425SSebastian Fricke return ret;
360*02a8b425SSebastian Fricke
361*02a8b425SSebastian Fricke /* Mark position of the frame cropping flag */
362*02a8b425SSebastian Fricke pos = sps.pos;
363*02a8b425SSebastian Fricke frame_cropping_flag = ret = rbsp_read_bit(&sps);
364*02a8b425SSebastian Fricke if (ret < 0)
365*02a8b425SSebastian Fricke return ret;
366*02a8b425SSebastian Fricke if (frame_cropping_flag) {
367*02a8b425SSebastian Fricke unsigned int crop_left, crop_top;
368*02a8b425SSebastian Fricke
369*02a8b425SSebastian Fricke ret = rbsp_read_uev(&sps, &crop_left);
370*02a8b425SSebastian Fricke if (ret)
371*02a8b425SSebastian Fricke return ret;
372*02a8b425SSebastian Fricke ret = rbsp_read_uev(&sps, &crop_right);
373*02a8b425SSebastian Fricke if (ret)
374*02a8b425SSebastian Fricke return ret;
375*02a8b425SSebastian Fricke ret = rbsp_read_uev(&sps, &crop_top);
376*02a8b425SSebastian Fricke if (ret)
377*02a8b425SSebastian Fricke return ret;
378*02a8b425SSebastian Fricke ret = rbsp_read_uev(&sps, &crop_bottom);
379*02a8b425SSebastian Fricke if (ret)
380*02a8b425SSebastian Fricke return ret;
381*02a8b425SSebastian Fricke }
382*02a8b425SSebastian Fricke vui_parameters_present_flag = ret = rbsp_read_bit(&sps);
383*02a8b425SSebastian Fricke if (ret < 0)
384*02a8b425SSebastian Fricke return ret;
385*02a8b425SSebastian Fricke if (vui_parameters_present_flag) {
386*02a8b425SSebastian Fricke dev_err(ctx->fh.vdev->dev_parent,
387*02a8b425SSebastian Fricke "%s: Handling vui_parameters not implemented\n",
388*02a8b425SSebastian Fricke __func__);
389*02a8b425SSebastian Fricke return -EINVAL;
390*02a8b425SSebastian Fricke }
391*02a8b425SSebastian Fricke
392*02a8b425SSebastian Fricke crop_right = round_up(width, 16) - width;
393*02a8b425SSebastian Fricke crop_bottom = round_up(height, 16) - height;
394*02a8b425SSebastian Fricke crop_right /= 2;
395*02a8b425SSebastian Fricke if (frame_mbs_only_flag)
396*02a8b425SSebastian Fricke crop_bottom /= 2;
397*02a8b425SSebastian Fricke else
398*02a8b425SSebastian Fricke crop_bottom /= 4;
399*02a8b425SSebastian Fricke
400*02a8b425SSebastian Fricke
401*02a8b425SSebastian Fricke sps.size = max_size - 5;
402*02a8b425SSebastian Fricke sps.pos = pos;
403*02a8b425SSebastian Fricke frame_cropping_flag = 1;
404*02a8b425SSebastian Fricke ret = rbsp_write_bit(&sps, frame_cropping_flag);
405*02a8b425SSebastian Fricke if (ret)
406*02a8b425SSebastian Fricke return ret;
407*02a8b425SSebastian Fricke ret = rbsp_write_uev(&sps, 0); /* crop_left */
408*02a8b425SSebastian Fricke if (ret)
409*02a8b425SSebastian Fricke return ret;
410*02a8b425SSebastian Fricke ret = rbsp_write_uev(&sps, crop_right);
411*02a8b425SSebastian Fricke if (ret)
412*02a8b425SSebastian Fricke return ret;
413*02a8b425SSebastian Fricke ret = rbsp_write_uev(&sps, 0); /* crop_top */
414*02a8b425SSebastian Fricke if (ret)
415*02a8b425SSebastian Fricke return ret;
416*02a8b425SSebastian Fricke ret = rbsp_write_uev(&sps, crop_bottom);
417*02a8b425SSebastian Fricke if (ret)
418*02a8b425SSebastian Fricke return ret;
419*02a8b425SSebastian Fricke ret = rbsp_write_bit(&sps, 0); /* vui_parameters_present_flag */
420*02a8b425SSebastian Fricke if (ret)
421*02a8b425SSebastian Fricke return ret;
422*02a8b425SSebastian Fricke ret = rbsp_write_bit(&sps, 1);
423*02a8b425SSebastian Fricke if (ret)
424*02a8b425SSebastian Fricke return ret;
425*02a8b425SSebastian Fricke
426*02a8b425SSebastian Fricke *size = 5 + DIV_ROUND_UP(sps.pos, 8);
427*02a8b425SSebastian Fricke
428*02a8b425SSebastian Fricke return 0;
429*02a8b425SSebastian Fricke }
430