2 * Coda multi-standard codec IP - H.264 helper functions
4 * Copyright (C) 2012 Vista Silicon S.L.
5 * Javier Martin, <javier.martin@vista-silicon.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/kernel.h>
15 #include <linux/string.h>
16 #include <linux/videodev2.h>
19 static const u8 coda_filler_size[8] = { 0, 7, 14, 13, 12, 11, 10, 9 };
21 static const u8 *coda_find_nal_header(const u8 *buf, const u8 *end)
26 val = val << 8 | *buf++;
29 } while (val != 0x00000001);
34 int coda_sps_parse_profile(struct coda_ctx *ctx, struct vb2_buffer *vb)
36 const u8 *buf = vb2_plane_vaddr(vb, 0);
37 const u8 *end = buf + vb2_get_plane_payload(vb, 0);
41 buf = coda_find_nal_header(buf, end);
44 } while ((*buf++ & 0x1f) != 0x7);
46 ctx->params.h264_profile_idc = buf[0];
47 ctx->params.h264_level_idc = buf[2];
52 int coda_h264_filler_nal(int size, char *p)
62 memset(p + 5, 0xff, size - 6);
63 /* Add rbsp stop bit and trailing at the end */
69 int coda_h264_padding(int size, char *p)
74 diff = size - (size & ~0x7);
78 nal_size = coda_filler_size[diff];
79 coda_h264_filler_nal(nal_size, p);
84 int coda_h264_profile(int profile_idc)
86 switch (profile_idc) {
87 case 66: return V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE;
88 case 77: return V4L2_MPEG_VIDEO_H264_PROFILE_MAIN;
89 case 88: return V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED;
90 case 100: return V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
91 default: return -EINVAL;
95 int coda_h264_level(int level_idc)
98 case 10: return V4L2_MPEG_VIDEO_H264_LEVEL_1_0;
99 case 9: return V4L2_MPEG_VIDEO_H264_LEVEL_1B;
100 case 11: return V4L2_MPEG_VIDEO_H264_LEVEL_1_1;
101 case 12: return V4L2_MPEG_VIDEO_H264_LEVEL_1_2;
102 case 13: return V4L2_MPEG_VIDEO_H264_LEVEL_1_3;
103 case 20: return V4L2_MPEG_VIDEO_H264_LEVEL_2_0;
104 case 21: return V4L2_MPEG_VIDEO_H264_LEVEL_2_1;
105 case 22: return V4L2_MPEG_VIDEO_H264_LEVEL_2_2;
106 case 30: return V4L2_MPEG_VIDEO_H264_LEVEL_3_0;
107 case 31: return V4L2_MPEG_VIDEO_H264_LEVEL_3_1;
108 case 32: return V4L2_MPEG_VIDEO_H264_LEVEL_3_2;
109 case 40: return V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
110 case 41: return V4L2_MPEG_VIDEO_H264_LEVEL_4_1;
111 case 42: return V4L2_MPEG_VIDEO_H264_LEVEL_4_2;
112 case 50: return V4L2_MPEG_VIDEO_H264_LEVEL_5_0;
113 case 51: return V4L2_MPEG_VIDEO_H264_LEVEL_5_1;
114 default: return -EINVAL;
124 static inline int rbsp_read_bit(struct rbsp *rbsp)
126 int shift = 7 - (rbsp->pos % 8);
127 int ofs = rbsp->pos++ / 8;
129 if (ofs >= rbsp->size)
132 return (rbsp->buf[ofs] >> shift) & 1;
135 static inline int rbsp_write_bit(struct rbsp *rbsp, int bit)
137 int shift = 7 - (rbsp->pos % 8);
138 int ofs = rbsp->pos++ / 8;
140 if (ofs >= rbsp->size)
143 rbsp->buf[ofs] &= ~(1 << shift);
144 rbsp->buf[ofs] |= bit << shift;
149 static inline int rbsp_read_bits(struct rbsp *rbsp, int num, int *val)
157 for (i = 0; i < num; i++) {
158 ret = rbsp_read_bit(rbsp);
161 tmp |= ret << (num - i - 1);
170 static int rbsp_write_bits(struct rbsp *rbsp, int num, int value)
175 ret = rbsp_write_bit(rbsp, (value >> num) & 1);
183 static int rbsp_read_uev(struct rbsp *rbsp, unsigned int *val)
185 int leading_zero_bits = 0;
186 unsigned int tmp = 0;
189 while ((ret = rbsp_read_bit(rbsp)) == 0)
194 if (leading_zero_bits > 0) {
195 ret = rbsp_read_bits(rbsp, leading_zero_bits, &tmp);
201 *val = (1 << leading_zero_bits) - 1 + tmp;
206 static int rbsp_write_uev(struct rbsp *rbsp, unsigned int value)
211 int leading_zero_bits = fls(tmp) - 1;
213 for (i = 0; i < leading_zero_bits; i++) {
214 ret = rbsp_write_bit(rbsp, 0);
219 return rbsp_write_bits(rbsp, leading_zero_bits + 1, tmp);
222 static int rbsp_read_sev(struct rbsp *rbsp, int *val)
227 ret = rbsp_read_uev(rbsp, &tmp);
233 *val = (tmp + 1) / 2;
242 * coda_h264_sps_fixup - fixes frame cropping values in h.264 SPS
243 * @ctx: encoder context
244 * @width: visible width
245 * @height: visible height
246 * @buf: buffer containing h.264 SPS RBSP, starting with NAL header
247 * @size: modified RBSP size return value
248 * @max_size: available size in buf
250 * Rewrites the frame cropping values in an h.264 SPS RBSP correctly for the
251 * given visible width and height.
253 int coda_h264_sps_fixup(struct coda_ctx *ctx, int width, int height, char *buf,
254 int *size, int max_size)
257 unsigned int pic_order_cnt_type;
258 int pic_width_in_mbs_minus1, pic_height_in_map_units_minus1;
259 int frame_mbs_only_flag, frame_cropping_flag;
260 int vui_parameters_present_flag;
261 unsigned int crop_right, crop_bottom;
266 if (*size < 8 || *size >= max_size)
269 sps.buf = buf + 5; /* Skip NAL header */
270 sps.size = *size - 5;
272 profile_idc = sps.buf[0];
273 /* Skip constraint_set[0-5]_flag, reserved_zero_2bits */
277 /* seq_parameter_set_id */
278 ret = rbsp_read_uev(&sps, NULL);
282 if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 ||
283 profile_idc == 244 || profile_idc == 44 || profile_idc == 83 ||
284 profile_idc == 86 || profile_idc == 118 || profile_idc == 128 ||
285 profile_idc == 138 || profile_idc == 139 || profile_idc == 134 ||
286 profile_idc == 135) {
287 dev_err(ctx->fh.vdev->dev_parent,
288 "%s: Handling profile_idc %d not implemented\n",
289 __func__, profile_idc);
293 /* log2_max_frame_num_minus4 */
294 ret = rbsp_read_uev(&sps, NULL);
298 ret = rbsp_read_uev(&sps, &pic_order_cnt_type);
302 if (pic_order_cnt_type == 0) {
303 /* log2_max_pic_order_cnt_lsb_minus4 */
304 ret = rbsp_read_uev(&sps, NULL);
307 } else if (pic_order_cnt_type == 1) {
308 unsigned int i, num_ref_frames_in_pic_order_cnt_cycle;
310 /* delta_pic_order_always_zero_flag */
311 ret = rbsp_read_bit(&sps);
314 /* offset_for_non_ref_pic */
315 ret = rbsp_read_sev(&sps, NULL);
318 /* offset_for_top_to_bottom_field */
319 ret = rbsp_read_sev(&sps, NULL);
323 ret = rbsp_read_uev(&sps,
324 &num_ref_frames_in_pic_order_cnt_cycle);
327 for (i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++) {
328 /* offset_for_ref_frame */
329 ret = rbsp_read_sev(&sps, NULL);
335 /* max_num_ref_frames */
336 ret = rbsp_read_uev(&sps, NULL);
340 /* gaps_in_frame_num_value_allowed_flag */
341 ret = rbsp_read_bit(&sps);
344 ret = rbsp_read_uev(&sps, &pic_width_in_mbs_minus1);
347 ret = rbsp_read_uev(&sps, &pic_height_in_map_units_minus1);
350 frame_mbs_only_flag = ret = rbsp_read_bit(&sps);
353 if (!frame_mbs_only_flag) {
354 /* mb_adaptive_frame_field_flag */
355 ret = rbsp_read_bit(&sps);
359 /* direct_8x8_inference_flag */
360 ret = rbsp_read_bit(&sps);
364 /* Mark position of the frame cropping flag */
366 frame_cropping_flag = ret = rbsp_read_bit(&sps);
369 if (frame_cropping_flag) {
370 unsigned int crop_left, crop_top;
372 ret = rbsp_read_uev(&sps, &crop_left);
375 ret = rbsp_read_uev(&sps, &crop_right);
378 ret = rbsp_read_uev(&sps, &crop_top);
381 ret = rbsp_read_uev(&sps, &crop_bottom);
385 vui_parameters_present_flag = ret = rbsp_read_bit(&sps);
388 if (vui_parameters_present_flag) {
389 dev_err(ctx->fh.vdev->dev_parent,
390 "%s: Handling vui_parameters not implemented\n",
395 crop_right = round_up(width, 16) - width;
396 crop_bottom = round_up(height, 16) - height;
398 if (frame_mbs_only_flag)
404 sps.size = max_size - 5;
406 frame_cropping_flag = 1;
407 ret = rbsp_write_bit(&sps, frame_cropping_flag);
410 ret = rbsp_write_uev(&sps, 0); /* crop_left */
413 ret = rbsp_write_uev(&sps, crop_right);
416 ret = rbsp_write_uev(&sps, 0); /* crop_top */
419 ret = rbsp_write_uev(&sps, crop_bottom);
422 ret = rbsp_write_bit(&sps, 0); /* vui_parameters_present_flag */
425 ret = rbsp_write_bit(&sps, 1);
429 *size = 5 + DIV_ROUND_UP(sps.pos, 8);