1 /*
2 * Copyright 2015 Rockchip Electronics Co. LTD
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define MODULE_TAG "avsd_parse"
18
19 #include <string.h>
20 #include <stdlib.h>
21
22 #include "mpp_mem.h"
23 #include "mpp_packet_impl.h"
24 #include "hal_dec_task.h"
25
26 #include "avsd_api.h"
27 #include "avsd_parse.h"
28
get_sequence_header(BitReadCtx_t * bitctx,AvsdSeqHeader_t * vsh)29 static MPP_RET get_sequence_header(BitReadCtx_t *bitctx, AvsdSeqHeader_t *vsh)
30 {
31 MPP_RET ret = MPP_ERR_UNKNOW;
32 RK_U32 val_temp = 0;
33
34 READ_BITS(bitctx, 8, &vsh->profile_id);
35 //!< check profile_id
36 if (vsh->profile_id != 0x20 && vsh->profile_id != 0x48) {
37 ret = MPP_NOK;
38 mpp_err_f("profile_id 0x%02x is not supported.\n", vsh->profile_id);
39 goto __FAILED;
40 }
41 READ_BITS(bitctx, 8, &vsh->level_id);
42 if (vsh->level_id > 0x42) {
43 ret = MPP_NOK;
44 mpp_err_f("level_id 0x%02x is not supported.\n", vsh->level_id);
45 goto __FAILED;
46 }
47 READ_ONEBIT(bitctx, &vsh->progressive_sequence);
48 READ_BITS(bitctx, 14, &vsh->horizontal_size);
49 READ_BITS(bitctx, 14, &vsh->vertical_size);
50 READ_BITS(bitctx, 2, &vsh->chroma_format);
51 READ_BITS(bitctx, 3, &vsh->sample_precision);
52 READ_BITS(bitctx, 4, &vsh->aspect_ratio);
53 READ_BITS(bitctx, 4, &vsh->frame_rate_code);
54 READ_BITS(bitctx, 18, &val_temp); //!< bit_rate_high_18
55 vsh->bit_rate = val_temp << 12;
56 SKIP_BITS(bitctx, 1);
57 READ_BITS(bitctx, 12, &val_temp); //!< bit_rate_low_12
58 vsh->bit_rate += val_temp;
59 READ_ONEBIT(bitctx, &vsh->low_delay);
60 SKIP_BITS(bitctx, 1);
61 READ_BITS(bitctx, 18, &vsh->bbv_buffer_size);
62 READ_BITS(bitctx, 3, &val_temp); //!< reserve 3bits 000
63 if (val_temp) {
64 AVSD_DBG(AVSD_DBG_WARNNING, "reserver bits error.\n");
65 }
66 return ret = MPP_OK;
67 __BITREAD_ERR:
68 return ret = bitctx->ret;
69 __FAILED:
70 return ret;
71 }
72
gen_weight_quant_param(AvsdPicHeader_t * ph)73 static MPP_RET gen_weight_quant_param(AvsdPicHeader_t *ph)
74 {
75 RK_U32 i = 0;
76 RK_U32 *wqp = (RK_U32 *)ph->weighting_quant_param;
77
78 RK_U32 weighting_quant_param_default[] = { 128, 98, 106, 116, 116, 128 };
79 RK_U32 weighting_quant_param_base1[] = { 135, 143, 143, 160, 160, 213 };
80 RK_U32 weighting_quant_param_base2[] = { 128, 98, 106, 116, 116, 128 };
81
82 if (ph->weighting_quant_flag == 0) {
83 //!< needn't generate this param
84 for (i = 0; i < 6; i++) {
85 wqp[i] = 128;
86 }
87 return MPP_OK;
88 }
89
90 if (ph->weighting_quant_param_index == 0x0) {
91 for (i = 0; i < 6; i++) {
92 wqp[i] = weighting_quant_param_default[i];
93 }
94 } else if (ph->weighting_quant_param_index == 0x1) {
95 for (i = 0; i < 6; i++) {
96 wqp[i] = weighting_quant_param_base1[i] +
97 ph->weighting_quant_param_delta1[i];
98 }
99 } else if (ph->weighting_quant_param_index == 0x2) {
100 for (i = 0; i < 6; i++) {
101 wqp[i] = weighting_quant_param_base2[i] +
102 ph->weighting_quant_param_delta2[i];
103 }
104 } else {
105 //!< shouldn't happen
106 AVSD_DBG(AVSD_DBG_WARNNING, "Something went wrong.\n");
107 for (i = 0; i < 6; i++) {
108 wqp[i] = 128;
109 }
110 }
111
112 return MPP_OK;
113 }
114
get_extend_header(BitReadCtx_t * bitctx,AvsdSeqHeader_t * vsh,AvsdPicHeader_t * ph)115 static MPP_RET get_extend_header(BitReadCtx_t *bitctx, AvsdSeqHeader_t *vsh, AvsdPicHeader_t *ph)
116 {
117 MPP_RET ret = MPP_ERR_UNKNOW;
118 RK_U32 i = 0;
119
120 READ_ONEBIT(bitctx, &ph->loop_filter_disable);
121 if (!ph->loop_filter_disable) {
122 READ_ONEBIT(bitctx, &ph->loop_filter_parameter_flag);
123 if (ph->loop_filter_parameter_flag) {
124 READ_SE(bitctx, &ph->alpha_c_offset);
125 READ_SE(bitctx, &ph->beta_offset);
126 }
127 }
128 ph->chroma_quant_param_delta_cb = 0;
129 ph->chroma_quant_param_delta_cr = 0;
130 for (i = 0; i < 6; i++) {
131 ph->weighting_quant_param_delta1[i] = 0;
132 ph->weighting_quant_param_delta2[i] = 0;
133 }
134 if (vsh->profile_id == 0x48) {
135 READ_ONEBIT(bitctx, &ph->weighting_quant_flag);
136 if (ph->weighting_quant_flag) {
137 SKIP_BITS(bitctx, 1);
138 READ_ONEBIT(bitctx, &ph->chroma_quant_param_disable);
139 if (!ph->chroma_quant_param_disable) {
140 READ_SE(bitctx, &ph->chroma_quant_param_delta_cb);
141 READ_SE(bitctx, &ph->chroma_quant_param_delta_cr);
142 }
143 READ_BITS(bitctx, 2, &ph->weighting_quant_param_index);
144 READ_BITS(bitctx, 2, &ph->weighting_quant_model);
145 if (ph->weighting_quant_param_index == 1) {
146 for (i = 0; i < 6; i++) {
147 READ_SE(bitctx, &ph->weighting_quant_param_delta1[i]);
148 }
149 } else if (ph->weighting_quant_param_index == 2) {
150 for (i = 0; i < 6; i++) {
151 READ_SE(bitctx, &ph->weighting_quant_param_delta2[i]);
152 }
153 }
154 }
155 gen_weight_quant_param(ph); //!< generate wqP[m][6]
156
157 READ_ONEBIT(bitctx, &ph->aec_enable);
158 }
159
160 return ret = MPP_OK;
161 __BITREAD_ERR:
162 return ret = bitctx->ret;
163 }
164
get_seq_dispay_ext_header(BitReadCtx_t * bitctx,AvsdSeqExtHeader_t * ext)165 static MPP_RET get_seq_dispay_ext_header(BitReadCtx_t *bitctx, AvsdSeqExtHeader_t *ext)
166 {
167 MPP_RET ret = MPP_ERR_UNKNOW;
168 RK_U32 val_temp = 0;
169
170 READ_BITS(bitctx, 3, &ext->video_format);
171 READ_ONEBIT(bitctx, &ext->sample_range);
172 READ_ONEBIT(bitctx, &ext->color_description);
173
174 if (ext->color_description) {
175 READ_BITS(bitctx, 8, &ext->color_primaries);
176 READ_BITS(bitctx, 8, &ext->transfer_characteristics);
177 READ_BITS(bitctx, 8, &ext->matrix_coefficients);
178 }
179 READ_BITS(bitctx, 14, &ext->display_horizontalSize);
180 SKIP_BITS(bitctx, 1); //!< marker bit
181 READ_BITS(bitctx, 14, &ext->display_verticalSize);
182
183 READ_BITS(bitctx, 2, &val_temp); //!< reserve 2 bits
184 if (val_temp) {
185 AVSD_DBG(AVSD_DBG_WARNNING, "reserve bits not equal to zeros.\n");
186 }
187 return ret = MPP_OK;
188 __BITREAD_ERR:
189 return ret = bitctx->ret;
190 }
get_extension_header(BitReadCtx_t * bitctx,AvsdSeqExtHeader_t * ext)191 static MPP_RET get_extension_header(BitReadCtx_t *bitctx, AvsdSeqExtHeader_t *ext)
192 {
193 MPP_RET ret = MPP_ERR_UNKNOW;
194 RK_U32 val_temp = 0;
195
196 READ_BITS(bitctx, 4, &val_temp); //!< extension_start_code
197 switch (val_temp) {
198 case SEQUENCE_DISPLAY_EXTENTION:
199 FUN_CHECK(ret = get_seq_dispay_ext_header(bitctx, ext));
200 break;
201
202 default:
203 break;
204 }
205
206 return ret = MPP_OK;
207 __BITREAD_ERR:
208 return ret = bitctx->ret;
209 __FAILED:
210 return ret;
211 }
212
get_i_picture_header(BitReadCtx_t * bitctx,AvsdSeqHeader_t * vsh,AvsdPicHeader_t * ph)213 static MPP_RET get_i_picture_header(BitReadCtx_t *bitctx, AvsdSeqHeader_t *vsh, AvsdPicHeader_t *ph)
214 {
215 MPP_RET ret = MPP_ERR_UNKNOW;
216 RK_U32 val_temp = 0;
217 ph->picture_coding_type = I_PICTURE;
218
219 READ_BITS(bitctx, 16, &ph->bbv_delay);
220 if (vsh->profile_id == 0x48) {
221 SKIP_BITS(bitctx, 1);
222 READ_BITS(bitctx, 7, &ph->bbv_delay_extension);
223 }
224 READ_ONEBIT(bitctx, &ph->time_code_flag);
225 if (ph->time_code_flag) {
226 READ_BITS(bitctx, 24, &ph->time_code);
227 }
228
229 /* NOTE: only check version on correct I frame not found */
230 if (!vsh->version_checked) {
231 vsh->version = 0;
232 /* check stream version */
233 if (vsh->low_delay) {
234 vsh->version = 1;
235 } else {
236 SHOW_BITS(bitctx, 9, &val_temp);
237 if (!(val_temp & 1)) {
238 vsh->version = 1;
239 } else {
240 SHOW_BITS(bitctx, 11, &val_temp);
241 if (val_temp & 3)
242 vsh->version = 1;
243 }
244 }
245 }
246
247 if (vsh->version > 0)
248 SKIP_BITS(bitctx, 1); // marker bit
249
250 READ_BITS(bitctx, 8, &ph->picture_distance);
251
252 if (vsh->low_delay)
253 READ_UE(bitctx, &ph->bbv_check_times);
254
255 READ_ONEBIT(bitctx, &ph->progressive_frame);
256
257 ph->picture_structure = 1; // default frame
258 if (!ph->progressive_frame)
259 READ_ONEBIT(bitctx, &ph->picture_structure);
260
261 READ_ONEBIT(bitctx, &ph->top_field_first);
262 READ_ONEBIT(bitctx, &ph->repeat_first_field);
263 READ_ONEBIT(bitctx, &ph->fixed_picture_qp);
264 READ_BITS(bitctx, 6, &ph->picture_qp);
265 ph->skip_mode_flag = 0;
266 if (!ph->progressive_frame && !ph->picture_structure)
267 READ_ONEBIT(bitctx, &ph->skip_mode_flag);
268 READ_BITS(bitctx, 4, &val_temp); //!< reserve 4 bits
269 if (val_temp) {
270 AVSD_DBG(AVSD_DBG_WARNNING, "reserve bits not equal to zeros.\n");
271 }
272 ph->no_forward_reference_flag = 0;
273 ph->pb_field_enhanced_flag = 0;
274 ph->weighting_quant_flag = 0;
275 ph->aec_enable = 0;
276
277 FUN_CHECK(ret = get_extend_header(bitctx, vsh, ph));
278
279 return ret = MPP_OK;
280 __BITREAD_ERR:
281 return ret = bitctx->ret;
282 __FAILED:
283 return ret;
284 }
285
get_pb_picture_header(BitReadCtx_t * bitctx,AvsdSeqHeader_t * vsh,AvsdPicHeader_t * ph)286 static MPP_RET get_pb_picture_header(BitReadCtx_t *bitctx, AvsdSeqHeader_t *vsh, AvsdPicHeader_t *ph)
287 {
288 MPP_RET ret = MPP_ERR_UNKNOW;
289 RK_U32 val_temp = 0;
290
291 READ_BITS(bitctx, 16, &ph->bbv_delay);
292 if (vsh->profile_id == 0x48) {
293 SKIP_BITS(bitctx, 1);
294 READ_BITS(bitctx, 7, &ph->bbv_delay_extension);
295 }
296 READ_BITS(bitctx, 2, &ph->picture_coding_type);
297 READ_BITS(bitctx, 8, &ph->picture_distance);
298 if (vsh->low_delay) {
299 READ_UE(bitctx, &ph->bbv_check_times);
300 }
301 READ_ONEBIT(bitctx, &ph->progressive_frame);
302
303 if (!ph->progressive_frame) {
304 READ_ONEBIT(bitctx, &ph->picture_structure);
305 if (!ph->picture_structure) {
306 READ_ONEBIT(bitctx, &ph->advanced_pred_mode_disable);
307 }
308 } else {
309 ph->picture_structure = 1; //!< frame picture
310 }
311 READ_ONEBIT(bitctx, &ph->top_field_first);
312 READ_ONEBIT(bitctx, &ph->repeat_first_field);
313 READ_ONEBIT(bitctx, &ph->fixed_picture_qp);
314 READ_BITS(bitctx, 6, &ph->picture_qp);
315 if (!(ph->picture_coding_type == B_PICTURE && ph->picture_structure == P_PICTURE)) {
316 READ_ONEBIT(bitctx, &ph->picture_reference_flag);
317 }
318 READ_ONEBIT(bitctx, &ph->no_forward_reference_flag);
319 READ_ONEBIT(bitctx, &ph->pb_field_enhanced_flag);
320 if (vsh->profile_id != 0x48) {
321 ph->no_forward_reference_flag = 0;
322 ph->pb_field_enhanced_flag = 0;
323 }
324 ph->weighting_quant_flag = 0;
325 ph->aec_enable = 0;
326
327 READ_BITS(bitctx, 2, &val_temp); //!< reserve bits
328 if (val_temp) {
329 AVSD_DBG(AVSD_DBG_WARNNING, "reserve bits not equal to zeros.\n");
330 }
331 READ_ONEBIT(bitctx, &ph->skip_mode_flag);
332
333 FUN_CHECK(ret = get_extend_header(bitctx, vsh, ph));
334
335 return ret = MPP_OK;
336 __BITREAD_ERR:
337 return ret = bitctx->ret;
338 __FAILED:
339 return ret;
340 }
341
reset_one_save(AvsdFrame_t * p)342 static void reset_one_save(AvsdFrame_t *p)
343 {
344 if (p) {
345 RK_U32 idx = p->idx;
346
347 memset(p, 0, sizeof(AvsdFrame_t));
348 p->idx = idx;
349 p->slot_idx = -1;
350 }
351 }
352
get_one_save(AvsdCtx_t * p_dec,HalDecTask * task)353 static AvsdFrame_t *get_one_save(AvsdCtx_t *p_dec, HalDecTask *task)
354 {
355 RK_U32 i = 0;
356 AvsdFrame_t *p_cur = NULL;
357
358 for (i = 0; i < MPP_ARRAY_ELEMS(p_dec->mem->save); i++) {
359 if (!p_dec->mem->save[i].valid) {
360 p_dec->mem->save[i].valid = 1;
361 p_cur = &p_dec->mem->save[i];
362 break;
363 }
364 }
365 if (!p_cur) {
366 mpp_err("mem_save dpb %d slots has not get\n", MPP_ARRAY_ELEMS(p_dec->mem->save));
367 goto __FAILED;
368 }
369 (void)task;
370 return p_cur;
371 __FAILED:
372 reset_one_save(p_cur);
373
374 return NULL;
375 }
376
set_frame_unref(AvsdCtx_t * pdec,AvsdFrame_t * p)377 static MPP_RET set_frame_unref(AvsdCtx_t *pdec, AvsdFrame_t *p)
378 {
379 if (p && p->slot_idx >= 0) {
380 mpp_buf_slot_clr_flag(pdec->frame_slots, p->slot_idx, SLOT_CODEC_USE);
381 reset_one_save(p);
382 }
383
384 return MPP_OK;
385 }
386
387
set_frame_output(AvsdCtx_t * p_dec,AvsdFrame_t * p)388 MPP_RET set_frame_output(AvsdCtx_t *p_dec, AvsdFrame_t *p)
389 {
390 if (p && p->slot_idx >= 0 && !p->had_display) {
391 mpp_buf_slot_set_flag(p_dec->frame_slots, p->slot_idx, SLOT_QUEUE_USE);
392 mpp_buf_slot_enqueue(p_dec->frame_slots, p->slot_idx, QUEUE_DISPLAY);
393 p->had_display = 1;
394 }
395
396 return MPP_OK;
397 }
398
399 /*!
400 ***********************************************************************
401 * \brief
402 * reset decoder parameters
403 ***********************************************************************
404 */
avsd_reset_parameters(AvsdCtx_t * p_dec)405 MPP_RET avsd_reset_parameters(AvsdCtx_t *p_dec)
406 {
407 RK_U32 i = 0;
408
409 set_frame_output(p_dec, p_dec->dpb[1]);
410 set_frame_output(p_dec, p_dec->dpb[0]);
411 set_frame_output(p_dec, p_dec->cur);
412 set_frame_unref(p_dec, p_dec->dpb[1]);
413 set_frame_unref(p_dec, p_dec->dpb[0]);
414 set_frame_unref(p_dec, p_dec->cur);
415
416 p_dec->cur = NULL;
417 p_dec->dpb[0] = NULL;
418 p_dec->dpb[1] = NULL;
419
420 p_dec->vsh.version_checked = 0;
421
422 for (i = 0; i < MPP_ARRAY_ELEMS(p_dec->mem->save); i++) {
423 AvsdFrame_t *frm = &p_dec->mem->save[i];
424
425 memset(frm, 0, sizeof(*frm));
426 frm->idx = i;
427 frm->slot_idx = -1;
428 }
429
430 return MPP_OK;
431 }
432
433 /*!
434 ***********************************************************************
435 * \brief
436 * set refer
437 ***********************************************************************
438 */
avsd_set_dpb(AvsdCtx_t * p_dec,HalDecTask * task)439 MPP_RET avsd_set_dpb(AvsdCtx_t *p_dec, HalDecTask *task)
440 {
441 MppFrame mframe = NULL;
442 RK_S32 slot_idx = -1;
443 AvsdFrame_t *p_cur = p_dec->cur;
444
445 //!< set current dpb for decode
446 mpp_buf_slot_get_unused(p_dec->frame_slots, &slot_idx);
447 if (slot_idx < 0) {
448 AVSD_DBG(AVSD_DBG_WARNNING, "error, buf_slot has not get.\n");
449 goto __FAILED;
450 }
451 //!< init current frame data
452 p_cur->slot_idx = slot_idx;
453 p_cur->pic_type = p_dec->ph.picture_coding_type;
454 p_cur->width = p_dec->vsh.horizontal_size;
455 p_cur->height = p_dec->vsh.vertical_size;
456 p_cur->hor_stride = MPP_ALIGN(p_dec->vsh.horizontal_size, 16);
457 p_cur->ver_stride = MPP_ALIGN(p_dec->vsh.vertical_size, 16);
458 p_cur->pts = mpp_packet_get_pts(task->input_packet);
459 p_cur->dts = mpp_packet_get_dts(task->input_packet);
460 //!< set frame info
461 mpp_frame_init(&mframe);
462 mpp_frame_set_fmt(mframe, MPP_FMT_YUV420SP);
463 mpp_frame_set_hor_stride(mframe, p_cur->hor_stride); // before crop
464 mpp_frame_set_ver_stride(mframe, p_cur->ver_stride);
465 mpp_frame_set_width(mframe, p_cur->width); // after crop
466 mpp_frame_set_height(mframe, p_cur->height);
467 mpp_frame_set_pts(mframe, p_cur->pts);
468 mpp_frame_set_dts(mframe, p_cur->dts);
469 if (p_dec->ph.picture_structure) { //!< data combine 2 field
470 p_cur->frame_mode = MPP_FRAME_FLAG_PAIRED_FIELD;
471 if (p_dec->ph.top_field_first) {
472 p_cur->frame_mode |= MPP_FRAME_FLAG_TOP_FIRST;
473 } else {
474 p_cur->frame_mode |= MPP_FRAME_FLAG_BOT_FIRST;
475 }
476 } else { //!< frame picture
477 p_cur->frame_mode = MPP_FRAME_FLAG_FRAME;
478
479 if (p_dec->init.cfg->base.enable_vproc & MPP_VPROC_MODE_DETECTION) {
480 p_cur->frame_mode |= MPP_FRAME_FLAG_DEINTERLACED;
481 }
482 }
483 mpp_frame_set_mode(mframe, p_cur->frame_mode);
484 mpp_buf_slot_set_prop(p_dec->frame_slots, slot_idx, SLOT_FRAME, mframe);
485 mpp_frame_deinit(&mframe);
486
487 mpp_buf_slot_set_flag(p_dec->frame_slots, p_cur->slot_idx, SLOT_CODEC_USE);
488 mpp_buf_slot_set_flag(p_dec->frame_slots, p_cur->slot_idx, SLOT_HAL_OUTPUT);
489
490 //!< set task
491 task->output = p_dec->cur->slot_idx;
492 //!< set task refers
493 if (p_dec->dpb[0] && p_dec->dpb[0]->slot_idx >= 0 &&
494 (p_dec->dpb[0]->slot_idx != p_dec->cur->slot_idx)) {
495 mpp_buf_slot_set_flag(p_dec->frame_slots, p_dec->dpb[0]->slot_idx, SLOT_HAL_INPUT);
496 if (p_dec->ph.picture_coding_type == B_PICTURE) {
497 task->refer[1] = p_dec->dpb[0]->slot_idx;
498 } else {
499 task->refer[0] = p_dec->dpb[0]->slot_idx;
500 }
501 }
502 if (p_dec->dpb[1] && p_dec->dpb[1]->slot_idx >= 0 &&
503 (p_dec->dpb[1]->slot_idx != p_dec->cur->slot_idx)) {
504 mpp_buf_slot_set_flag(p_dec->frame_slots, p_dec->dpb[1]->slot_idx, SLOT_HAL_INPUT);
505 if (p_dec->ph.picture_coding_type == B_PICTURE) {
506 task->refer[0] = p_dec->dpb[1]->slot_idx;
507 } else {
508 task->refer[1] = p_dec->dpb[1]->slot_idx;
509 }
510 }
511
512 //!< set ref flag and mark error
513 if (p_dec->ph.picture_coding_type == I_PICTURE) {
514 task->flags.used_for_ref = 1;
515 task->flags.ref_err = 0;
516 } else if (p_dec->ph.picture_coding_type == P_PICTURE) {
517 task->flags.used_for_ref = 1;
518 if (task->refer[0] >= 0) {
519 mpp_buf_slot_get_prop(p_dec->frame_slots, task->refer[0], SLOT_FRAME_PTR, &mframe);
520 if (mframe)
521 task->flags.ref_err |= mpp_frame_get_errinfo(mframe);
522 }
523 } else if (p_dec->ph.picture_coding_type == B_PICTURE) {
524 task->flags.used_for_ref = 0;
525 if (task->refer[0] >= 0) {
526 mpp_buf_slot_get_prop(p_dec->frame_slots, task->refer[0], SLOT_FRAME_PTR, &mframe);
527 if (mframe)
528 task->flags.ref_err |= mpp_frame_get_errinfo(mframe);
529 }
530 if (task->refer[1] >= 0) {
531 mpp_buf_slot_get_prop(p_dec->frame_slots, task->refer[1], SLOT_FRAME_PTR, &mframe);
532 if (mframe)
533 task->flags.ref_err |= mpp_frame_get_errinfo(mframe);
534 }
535 }
536
537 return MPP_OK;
538 __FAILED:
539 return MPP_NOK;
540 }
541
542 /*!
543 ***********************************************************************
544 * \brief
545 * commit buffer to hal
546 ***********************************************************************
547 */
avsd_commit_syntaxs(AvsdSyntax_t * syn,HalDecTask * task)548 MPP_RET avsd_commit_syntaxs(AvsdSyntax_t *syn, HalDecTask *task)
549 {
550 task->syntax.number = 1;
551 task->syntax.data = syn;
552
553 return MPP_OK;
554 }
555
556 /*!
557 ***********************************************************************
558 * \brief
559 * commit buffer to hal
560 ***********************************************************************
561 */
avsd_update_dpb(AvsdCtx_t * p_dec)562 MPP_RET avsd_update_dpb(AvsdCtx_t *p_dec)
563 {
564 if (p_dec->ph.picture_coding_type != B_PICTURE) {
565 set_frame_output(p_dec, p_dec->dpb[0]);
566 set_frame_unref(p_dec, p_dec->dpb[1]);
567 p_dec->dpb[1] = p_dec->dpb[0];
568 p_dec->dpb[0] = p_dec->cur;
569 p_dec->cur = NULL;
570 } else {
571 set_frame_output(p_dec, p_dec->cur);
572 set_frame_unref(p_dec, p_dec->cur);
573 p_dec->cur = NULL;
574 }
575
576 return MPP_OK;
577 }
578
579 /*!
580 ***********************************************************************
581 * \brief
582 * fill parameters
583 ***********************************************************************
584 */
avsd_fill_parameters(AvsdCtx_t * p_dec,AvsdSyntax_t * syn)585 MPP_RET avsd_fill_parameters(AvsdCtx_t *p_dec, AvsdSyntax_t *syn)
586 {
587 RK_S32 i = 0;
588 PicParams_Avsd *pp = &syn->pp;
589
590 //!< sequence header
591 pp->profileId = p_dec->vsh.profile_id;
592 pp->levelId = p_dec->vsh.level_id;
593 pp->progressiveSequence = p_dec->vsh.progressive_sequence;
594 pp->horizontalSize = p_dec->vsh.horizontal_size;
595 pp->verticalSize = p_dec->vsh.vertical_size;
596 pp->chromaFormat = p_dec->vsh.chroma_format;
597 pp->aspectRatio = p_dec->vsh.aspect_ratio;
598 pp->frameRateCode = p_dec->vsh.frame_rate_code;
599 pp->bitRateValue = p_dec->vsh.bit_rate;
600 pp->lowDelay = p_dec->vsh.low_delay;
601 pp->bbvBufferSize = p_dec->vsh.bbv_buffer_size;
602
603 //!< sequence display extension header
604 pp->videoFormat = p_dec->ext.video_format;
605 pp->sampleRange = p_dec->ext.sample_range;
606 pp->colorDescription = p_dec->ext.color_description;
607 pp->colorPrimaries = p_dec->ext.color_primaries;
608 pp->transferCharacteristics = p_dec->ext.transfer_characteristics;
609 pp->matrixCoefficients = p_dec->ext.matrix_coefficients;
610 pp->displayHorizontalSize = p_dec->ext.display_horizontalSize;
611 pp->displayVerticalSize = p_dec->ext.display_verticalSize;
612
613 //!< picture header
614 pp->picCodingType = p_dec->ph.picture_coding_type;
615 pp->bbvDelay = p_dec->ph.bbv_delay;
616 pp->bbvDelayExtension = p_dec->ph.bbv_delay_extension;
617 pp->timeCodeFlag = p_dec->ph.time_code_flag;
618 pp->timeCode = p_dec->ph.time_code;
619
620 pp->pictureDistance = p_dec->ph.picture_distance;
621 pp->progressiveFrame = p_dec->ph.progressive_frame;
622 pp->pictureStructure = p_dec->ph.picture_structure;
623 pp->advancedPredModeDisable = p_dec->ph.advanced_pred_mode_disable;
624 pp->topFieldFirst = p_dec->ph.top_field_first;
625 pp->repeatFirstField = p_dec->ph.repeat_first_field;
626 pp->fixedPictureQp = p_dec->ph.fixed_picture_qp;
627 pp->pictureQp = p_dec->ph.picture_qp;
628 pp->pictureReferenceFlag = p_dec->ph.picture_reference_flag;
629 pp->skipModeFlag = p_dec->ph.skip_mode_flag;
630 pp->loopFilterDisable = p_dec->ph.loop_filter_disable;
631 pp->alphaOffset = p_dec->ph.alpha_c_offset;
632 pp->betaOffset = p_dec->ph.beta_offset;
633
634 //!< weighting quant, AVS Plus stuff
635 pp->weightingQuantFlag = p_dec->ph.weighting_quant_flag;
636 pp->chromaQuantParamDisable = p_dec->ph.chroma_quant_param_disable;
637 pp->chromaQuantParamDeltaCb = p_dec->ph.chroma_quant_param_delta_cb;
638 pp->chromaQuantParamDeltaCr = p_dec->ph.chroma_quant_param_delta_cr;
639 pp->weightingQuantParamIndex = p_dec->ph.weighting_quant_param_index;
640 pp->weightingQuantModel = p_dec->ph.weighting_quant_model;
641 for (i = 0; i < 6; i++) {
642 pp->weightingQuantParamDelta1[i] = p_dec->ph.weighting_quant_param_delta1[i];
643 pp->weightingQuantParamDelta2[i] = p_dec->ph.weighting_quant_param_delta2[i];
644 pp->weightingQuantParam[i] = p_dec->ph.weighting_quant_param[i];
645 }
646 //!< advance entropy coding
647 pp->aecEnable = p_dec->ph.aec_enable;
648
649 //!< picture enhance
650 pp->noForwardReferenceFlag = p_dec->ph.no_forward_reference_flag;
651 pp->pbFieldEnhancedFlag = p_dec->ph.pb_field_enhanced_flag;
652
653 //!< set stream offset
654 syn->bitstream_size = p_dec->cur->stream_len;
655 syn->bitstream_offset = p_dec->cur->stream_offset;
656
657 return MPP_OK;
658 }
659 /*!
660 ***********************************************************************
661 * \brief
662 * prepare function for parser
663 ***********************************************************************
664 */
avsd_parser_split(AvsdCtx_t * p,MppPacket * dst,MppPacket * src)665 MPP_RET avsd_parser_split(AvsdCtx_t *p, MppPacket *dst, MppPacket *src)
666 {
667 MPP_RET ret = MPP_NOK;
668 AVSD_PARSE_TRACE("In.\n");
669
670 RK_U8 *src_buf = (RK_U8 *)mpp_packet_get_pos(src);
671 RK_U32 src_len = (RK_U32)mpp_packet_get_length(src);
672 RK_U32 src_eos = mpp_packet_get_eos(src);
673 RK_S64 src_pts = mpp_packet_get_pts(src);
674 RK_S64 src_dts = mpp_packet_get_dts(src);
675 RK_U8 *dst_buf = (RK_U8 *)mpp_packet_get_data(dst);
676 RK_U32 dst_len = (RK_U32)mpp_packet_get_length(dst);
677 RK_U32 src_pos = 0;
678
679 // find the began of the vop
680 if (!p->vop_header_found) {
681 // add last startcode to the new frame data
682 if ((dst_len < sizeof(p->state))
683 && ((p->state & 0x00FFFFFF) == 0x000001)) {
684 dst_buf[0] = 0;
685 dst_buf[1] = 0;
686 dst_buf[2] = 1;
687 dst_len = 3;
688 }
689 while (src_pos < src_len) {
690 p->state = (p->state << 8) | src_buf[src_pos];
691 dst_buf[dst_len++] = src_buf[src_pos++];
692 if (p->state == I_PICUTRE_START_CODE ||
693 p->state == PB_PICUTRE_START_CODE) {
694 p->vop_header_found = 1;
695 mpp_packet_set_pts(dst, src_pts);
696 mpp_packet_set_dts(dst, src_dts);
697 break;
698 }
699 }
700 }
701
702 // find the end of the vop
703 if (p->vop_header_found) {
704 while (src_pos < src_len) {
705 p->state = (p->state << 8) | src_buf[src_pos];
706 dst_buf[dst_len++] = src_buf[src_pos++];
707 if ((p->state & 0x00FFFFFF) == 0x000001) {
708 if (src_buf[src_pos] > (SLICE_MAX_START_CODE & 0xFF) &&
709 src_buf[src_pos] != (USER_DATA_CODE & 0xFF)) {
710 dst_len -= 3;
711 p->vop_header_found = 0;
712 ret = MPP_OK; // split complete
713 break;
714 }
715 }
716 }
717 }
718 // the last packet
719 if (src_eos && src_pos >= src_len) {
720 mpp_packet_set_eos(dst);
721 ret = MPP_OK;
722 }
723
724 AVSD_DBG(AVSD_DBG_INPUT, "[pkt_in] vop_header_found= %d, dst_len=%d, src_pos=%d\n",
725 p->vop_header_found, dst_len, src_pos);
726 // reset the src and dst
727 mpp_packet_set_length(dst, dst_len);
728 mpp_packet_set_pos(src, src_buf + src_pos);
729
730 AVSD_PARSE_TRACE("out.\n");
731
732 return ret;
733 }
734 /*!
735 ***********************************************************************
736 * \brief
737 * parse stream which ha function for parser
738 ***********************************************************************
739 */
avsd_parse_stream(AvsdCtx_t * p_dec,HalDecTask * task)740 MPP_RET avsd_parse_stream(AvsdCtx_t *p_dec, HalDecTask *task)
741 {
742 MPP_RET ret = MPP_ERR_UNKNOW;
743 RK_U32 startcode = 0xFF;
744 RK_U32 pic_type = 0;
745 RK_U32 got_slice = 0;
746
747 RK_U8 *data = (RK_U8 *)mpp_packet_get_data(task->input_packet);
748 RK_S32 length = (RK_S32)mpp_packet_get_length(task->input_packet);
749
750 mpp_set_bitread_ctx(p_dec->bx, data, length);
751 AVSD_DBG(AVSD_DBG_SYNTAX, "bytes_left_=%d\n", p_dec->bx->bytes_left_);
752 while (p_dec->bx->bytes_left_ && !got_slice) {
753 RK_S32 tmp = 0;
754 mpp_align_get_bits(p_dec->bx);
755 mpp_read_bits(p_dec->bx, 8, &tmp);
756 startcode = (startcode << 8) | tmp;
757 if ((startcode & 0xFFFFFF00) != 0x100)
758 continue;
759 AVSD_DBG(AVSD_DBG_SYNTAX, "startcode=%08x\n", startcode);
760
761 // when has not got sequence header, then do nothing
762 if (!p_dec->got_vsh &&
763 startcode != VIDEO_SEQUENCE_START_CODE) {
764 AVSD_DBG(AVSD_DBG_WARNNING, "when has not got sequence header, then do nothing\n");
765 continue;
766 }
767 //continue;
768 switch (startcode) {
769 case VIDEO_SEQUENCE_START_CODE:
770 ret = get_sequence_header(p_dec->bx, &p_dec->vsh);
771 if (ret == MPP_OK) {
772 p_dec->got_vsh = 1;
773 }
774 AVSD_DBG(AVSD_DBG_WARNNING, "got vsh %d\n", p_dec->got_vsh);
775 break;
776 case VIDEO_SEQUENCE_END_CODE:
777 break;
778 case USER_DATA_CODE:
779 break;
780 case VIDEO_EDIT_CODE:
781 p_dec->vec_flag = 0;
782 break;
783 case I_PICUTRE_START_CODE:
784 AVSD_DBG(AVSD_DBG_WARNNING, "got I picture start code\n");
785 if (!p_dec->got_keyframe) {
786 avsd_reset_parameters(p_dec);
787 p_dec->got_keyframe = 1;
788 }
789 ret = get_i_picture_header(p_dec->bx, &p_dec->vsh, &p_dec->ph);
790 if (ret == MPP_OK) {
791 p_dec->cur = get_one_save(p_dec, task);
792 p_dec->got_ph = 1;
793 }
794 p_dec->cur->pic_type = pic_type = I_PICTURE;
795 p_dec->vec_flag++;
796 break;
797 case EXTENSION_START_CODE:
798 ret = get_extension_header(p_dec->bx, &p_dec->ext);
799 break;
800 case PB_PICUTRE_START_CODE:
801 AVSD_DBG(AVSD_DBG_WARNNING, "got PB picture start code\n");
802 if (!p_dec->got_keyframe) {
803 avsd_reset_parameters(p_dec);
804 break;
805 }
806 ret = get_pb_picture_header(p_dec->bx, &p_dec->vsh, &p_dec->ph);
807 if (ret == MPP_OK) {
808 p_dec->cur = get_one_save(p_dec, task);
809 p_dec->got_ph = 1;
810 }
811 p_dec->cur->pic_type = pic_type = p_dec->ph.picture_coding_type;
812 p_dec->vec_flag += (p_dec->vec_flag == 1 && pic_type == P_PICTURE);
813 break;
814 default:
815 if (p_dec->cur
816 && startcode >= SLICE_MIN_START_CODE
817 && startcode <= SLICE_MAX_START_CODE) {
818 got_slice = 1;
819 p_dec->cur->stream_len = length;
820 p_dec->cur->stream_offset = p_dec->bx->used_bits / 8 - 4;
821 task->valid = p_dec->got_vsh && p_dec->got_ph;
822 AVSD_DBG(AVSD_DBG_SYNTAX, "offset=%d,got_vsh=%d, got_ph=%d, task->valid=%d\n",
823 p_dec->cur->stream_offset, p_dec->got_vsh, p_dec->got_ph, task->valid);
824 }
825
826 if (p_dec->disable_error)
827 break;
828
829 if ((pic_type == P_PICTURE && !p_dec->dpb[0]) ||
830 (pic_type == B_PICTURE && !p_dec->dpb[0]) ||
831 (pic_type == B_PICTURE && !p_dec->dpb[1] && !p_dec->vsh.low_delay) ||
832 (pic_type == P_PICTURE && p_dec->vec_flag < 1) ||
833 (pic_type == B_PICTURE && p_dec->vec_flag < 2)) {
834 AVSD_DBG(AVSD_DBG_REF, "missing refer frame.\n");
835 if (!p_dec->disable_error)
836 goto __FAILED;
837 }
838 break;
839 }
840 }
841
842 if (!task->valid)
843 goto __FAILED;
844
845 return MPP_OK;
846 __FAILED:
847 task->valid = 0;
848 reset_one_save(p_dec->cur);
849 return MPP_NOK;
850 }
851