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
266 ph->skip_mode_flag = 0;
267 if (!ph->progressive_frame && !ph->picture_structure)
268 READ_ONEBIT(bitctx, &ph->skip_mode_flag);
269
270 READ_BITS(bitctx, 4, &val_temp); //!< reserve 4 bits
271 if (val_temp) {
272 AVSD_DBG(AVSD_DBG_WARNNING, "reserve bits not equal to zeros.\n");
273 }
274 ph->no_forward_reference_flag = 0;
275 ph->pb_field_enhanced_flag = 0;
276 ph->weighting_quant_flag = 0;
277 ph->aec_enable = 0;
278
279 FUN_CHECK(ret = get_extend_header(bitctx, vsh, ph));
280
281 return ret = MPP_OK;
282 __BITREAD_ERR:
283 return ret = bitctx->ret;
284 __FAILED:
285 return ret;
286 }
287
get_pb_picture_header(BitReadCtx_t * bitctx,AvsdSeqHeader_t * vsh,AvsdPicHeader_t * ph)288 static MPP_RET get_pb_picture_header(BitReadCtx_t *bitctx, AvsdSeqHeader_t *vsh, AvsdPicHeader_t *ph)
289 {
290 MPP_RET ret = MPP_ERR_UNKNOW;
291 RK_U32 val_temp = 0;
292
293 READ_BITS(bitctx, 16, &ph->bbv_delay);
294 if (vsh->profile_id == 0x48) {
295 SKIP_BITS(bitctx, 1);
296 READ_BITS(bitctx, 7, &ph->bbv_delay_extension);
297 }
298 READ_BITS(bitctx, 2, &ph->picture_coding_type);
299 READ_BITS(bitctx, 8, &ph->picture_distance);
300 if (vsh->low_delay) {
301 READ_UE(bitctx, &ph->bbv_check_times);
302 }
303 READ_ONEBIT(bitctx, &ph->progressive_frame);
304
305 if (!ph->progressive_frame) {
306 READ_ONEBIT(bitctx, &ph->picture_structure);
307 if (!ph->picture_structure) {
308 READ_ONEBIT(bitctx, &ph->advanced_pred_mode_disable);
309 }
310 } else {
311 ph->picture_structure = 1; //!< frame picture
312 }
313 READ_ONEBIT(bitctx, &ph->top_field_first);
314 READ_ONEBIT(bitctx, &ph->repeat_first_field);
315 READ_ONEBIT(bitctx, &ph->fixed_picture_qp);
316 READ_BITS(bitctx, 6, &ph->picture_qp);
317 if (!(ph->picture_coding_type == B_PICTURE && ph->picture_structure == P_PICTURE)) {
318 READ_ONEBIT(bitctx, &ph->picture_reference_flag);
319 }
320 READ_ONEBIT(bitctx, &ph->no_forward_reference_flag);
321 READ_ONEBIT(bitctx, &ph->pb_field_enhanced_flag);
322 if (vsh->profile_id != 0x48) {
323 ph->no_forward_reference_flag = 0;
324 ph->pb_field_enhanced_flag = 0;
325 }
326 ph->weighting_quant_flag = 0;
327 ph->aec_enable = 0;
328
329 READ_BITS(bitctx, 2, &val_temp); //!< reserve bits
330 if (val_temp) {
331 AVSD_DBG(AVSD_DBG_WARNNING, "reserve bits not equal to zeros.\n");
332 }
333 READ_ONEBIT(bitctx, &ph->skip_mode_flag);
334
335 FUN_CHECK(ret = get_extend_header(bitctx, vsh, ph));
336
337 return ret = MPP_OK;
338 __BITREAD_ERR:
339 return ret = bitctx->ret;
340 __FAILED:
341 return ret;
342 }
343
reset_one_save(AvsdFrame_t * p)344 static void reset_one_save(AvsdFrame_t *p)
345 {
346 if (p) {
347 RK_U32 idx = p->idx;
348
349 memset(p, 0, sizeof(AvsdFrame_t));
350 p->idx = idx;
351 p->slot_idx = -1;
352 }
353 }
354
get_one_save(AvsdCtx_t * p_dec,HalDecTask * task)355 static AvsdFrame_t *get_one_save(AvsdCtx_t *p_dec, HalDecTask *task)
356 {
357 RK_U32 i = 0;
358 AvsdFrame_t *p_cur = NULL;
359
360 for (i = 0; i < MPP_ARRAY_ELEMS(p_dec->mem->save); i++) {
361 if (!p_dec->mem->save[i].valid) {
362 p_dec->mem->save[i].valid = 1;
363 p_cur = &p_dec->mem->save[i];
364 break;
365 }
366 }
367 if (!p_cur) {
368 mpp_err("mem_save dpb %d slots has not get\n", MPP_ARRAY_ELEMS(p_dec->mem->save));
369 goto __FAILED;
370 }
371 (void)task;
372 return p_cur;
373 __FAILED:
374 reset_one_save(p_cur);
375
376 return NULL;
377 }
378
set_frame_unref(AvsdCtx_t * pdec,AvsdFrame_t * p)379 static MPP_RET set_frame_unref(AvsdCtx_t *pdec, AvsdFrame_t *p)
380 {
381 if (p && p->slot_idx >= 0) {
382 mpp_buf_slot_clr_flag(pdec->frame_slots, p->slot_idx, SLOT_CODEC_USE);
383 reset_one_save(p);
384 }
385
386 return MPP_OK;
387 }
388
389
set_frame_output(AvsdCtx_t * p_dec,AvsdFrame_t * p)390 MPP_RET set_frame_output(AvsdCtx_t *p_dec, AvsdFrame_t *p)
391 {
392 if (p && p->slot_idx >= 0 && !p->had_display) {
393 mpp_buf_slot_set_flag(p_dec->frame_slots, p->slot_idx, SLOT_QUEUE_USE);
394 mpp_buf_slot_enqueue(p_dec->frame_slots, p->slot_idx, QUEUE_DISPLAY);
395 p->had_display = 1;
396 }
397
398 return MPP_OK;
399 }
400
401 /*!
402 ***********************************************************************
403 * \brief
404 * reset decoder parameters
405 ***********************************************************************
406 */
avsd_reset_parameters(AvsdCtx_t * p_dec)407 MPP_RET avsd_reset_parameters(AvsdCtx_t *p_dec)
408 {
409 RK_U32 i = 0;
410
411 set_frame_output(p_dec, p_dec->dpb[1]);
412 set_frame_output(p_dec, p_dec->dpb[0]);
413 set_frame_output(p_dec, p_dec->cur);
414 set_frame_unref(p_dec, p_dec->dpb[1]);
415 set_frame_unref(p_dec, p_dec->dpb[0]);
416 set_frame_unref(p_dec, p_dec->cur);
417
418 p_dec->cur = NULL;
419 p_dec->dpb[0] = NULL;
420 p_dec->dpb[1] = NULL;
421
422 p_dec->vsh.version_checked = 0;
423
424 for (i = 0; i < MPP_ARRAY_ELEMS(p_dec->mem->save); i++) {
425 AvsdFrame_t *frm = &p_dec->mem->save[i];
426
427 memset(frm, 0, sizeof(*frm));
428 frm->idx = i;
429 frm->slot_idx = -1;
430 }
431
432 return MPP_OK;
433 }
434
435 /*!
436 ***********************************************************************
437 * \brief
438 * set refer
439 ***********************************************************************
440 */
avsd_set_dpb(AvsdCtx_t * p_dec,HalDecTask * task)441 MPP_RET avsd_set_dpb(AvsdCtx_t *p_dec, HalDecTask *task)
442 {
443 MppFrame mframe = NULL;
444 RK_S32 slot_idx = -1;
445 AvsdFrame_t *p_cur = p_dec->cur;
446
447 //!< set current dpb for decode
448 mpp_buf_slot_get_unused(p_dec->frame_slots, &slot_idx);
449 if (slot_idx < 0) {
450 AVSD_DBG(AVSD_DBG_WARNNING, "error, buf_slot has not get.\n");
451 goto __FAILED;
452 }
453 //!< init current frame data
454 p_cur->slot_idx = slot_idx;
455 p_cur->pic_type = p_dec->ph.picture_coding_type;
456 p_cur->width = p_dec->vsh.horizontal_size;
457 p_cur->height = p_dec->vsh.vertical_size;
458 p_cur->hor_stride = MPP_ALIGN(p_dec->vsh.horizontal_size, 16);
459 p_cur->ver_stride = MPP_ALIGN(p_dec->vsh.vertical_size, 16);
460 p_cur->pts = mpp_packet_get_pts(task->input_packet);
461 p_cur->dts = mpp_packet_get_dts(task->input_packet);
462 //!< set frame info
463 mpp_frame_init(&mframe);
464 mpp_frame_set_fmt(mframe, MPP_FMT_YUV420SP);
465 mpp_frame_set_hor_stride(mframe, p_cur->hor_stride); // before crop
466 mpp_frame_set_ver_stride(mframe, p_cur->ver_stride);
467 mpp_frame_set_width(mframe, p_cur->width); // after crop
468 mpp_frame_set_height(mframe, p_cur->height);
469 mpp_frame_set_pts(mframe, p_cur->pts);
470 mpp_frame_set_dts(mframe, p_cur->dts);
471 if (p_dec->ph.picture_structure) { //!< data combine 2 field
472 p_cur->frame_mode = MPP_FRAME_FLAG_PAIRED_FIELD;
473 if (p_dec->ph.top_field_first) {
474 p_cur->frame_mode |= MPP_FRAME_FLAG_TOP_FIRST;
475 } else {
476 p_cur->frame_mode |= MPP_FRAME_FLAG_BOT_FIRST;
477 }
478 } else { //!< frame picture
479 p_cur->frame_mode = MPP_FRAME_FLAG_FRAME;
480 }
481 mpp_frame_set_mode(mframe, p_cur->frame_mode);
482 mpp_buf_slot_set_prop(p_dec->frame_slots, slot_idx, SLOT_FRAME, mframe);
483 mpp_frame_deinit(&mframe);
484
485 mpp_buf_slot_set_flag(p_dec->frame_slots, p_cur->slot_idx, SLOT_CODEC_USE);
486 mpp_buf_slot_set_flag(p_dec->frame_slots, p_cur->slot_idx, SLOT_HAL_OUTPUT);
487
488 //!< set task
489 task->output = p_dec->cur->slot_idx;
490 //!< set task refers
491 if (p_dec->dpb[0] && p_dec->dpb[0]->slot_idx >= 0 &&
492 (p_dec->dpb[0]->slot_idx != p_dec->cur->slot_idx)) {
493 mpp_buf_slot_set_flag(p_dec->frame_slots, p_dec->dpb[0]->slot_idx, SLOT_HAL_INPUT);
494 if (p_dec->ph.picture_coding_type == B_PICTURE) {
495 task->refer[1] = p_dec->dpb[0]->slot_idx;
496 } else {
497 task->refer[0] = p_dec->dpb[0]->slot_idx;
498 }
499 }
500 if (p_dec->dpb[1] && p_dec->dpb[1]->slot_idx >= 0 &&
501 (p_dec->dpb[1]->slot_idx != p_dec->cur->slot_idx)) {
502 mpp_buf_slot_set_flag(p_dec->frame_slots, p_dec->dpb[1]->slot_idx, SLOT_HAL_INPUT);
503 if (p_dec->ph.picture_coding_type == B_PICTURE) {
504 task->refer[0] = p_dec->dpb[1]->slot_idx;
505 } else {
506 task->refer[1] = p_dec->dpb[1]->slot_idx;
507 }
508 }
509
510 //!< set ref flag and mark error
511 if (p_dec->ph.picture_coding_type == I_PICTURE) {
512 task->flags.used_for_ref = 1;
513 task->flags.ref_err = 0;
514 } else if (p_dec->ph.picture_coding_type == P_PICTURE) {
515 task->flags.used_for_ref = 1;
516 if (task->refer[0] >= 0) {
517 mpp_buf_slot_get_prop(p_dec->frame_slots, task->refer[0], SLOT_FRAME_PTR, &mframe);
518 if (mframe)
519 task->flags.ref_err |= mpp_frame_get_errinfo(mframe);
520 }
521 } else if (p_dec->ph.picture_coding_type == B_PICTURE) {
522 task->flags.used_for_ref = 0;
523 if (task->refer[0] >= 0) {
524 mpp_buf_slot_get_prop(p_dec->frame_slots, task->refer[0], SLOT_FRAME_PTR, &mframe);
525 if (mframe)
526 task->flags.ref_err |= mpp_frame_get_errinfo(mframe);
527 }
528 if (task->refer[1] >= 0) {
529 mpp_buf_slot_get_prop(p_dec->frame_slots, task->refer[1], SLOT_FRAME_PTR, &mframe);
530 if (mframe)
531 task->flags.ref_err |= mpp_frame_get_errinfo(mframe);
532 }
533 }
534
535 return MPP_OK;
536 __FAILED:
537 return MPP_NOK;
538 }
539
540 /*!
541 ***********************************************************************
542 * \brief
543 * commit buffer to hal
544 ***********************************************************************
545 */
avsd_commit_syntaxs(AvsdSyntax_t * syn,HalDecTask * task)546 MPP_RET avsd_commit_syntaxs(AvsdSyntax_t *syn, HalDecTask *task)
547 {
548 task->syntax.number = 1;
549 task->syntax.data = syn;
550
551 return MPP_OK;
552 }
553
554 /*!
555 ***********************************************************************
556 * \brief
557 * commit buffer to hal
558 ***********************************************************************
559 */
avsd_update_dpb(AvsdCtx_t * p_dec)560 MPP_RET avsd_update_dpb(AvsdCtx_t *p_dec)
561 {
562 if (p_dec->ph.picture_coding_type != B_PICTURE) {
563 set_frame_output(p_dec, p_dec->dpb[0]);
564 set_frame_unref(p_dec, p_dec->dpb[1]);
565 p_dec->dpb[1] = p_dec->dpb[0];
566 p_dec->dpb[0] = p_dec->cur;
567 p_dec->cur = NULL;
568 } else {
569 set_frame_output(p_dec, p_dec->cur);
570 set_frame_unref(p_dec, p_dec->cur);
571 p_dec->cur = NULL;
572 }
573
574 return MPP_OK;
575 }
576
577 /*!
578 ***********************************************************************
579 * \brief
580 * fill parameters
581 ***********************************************************************
582 */
avsd_fill_parameters(AvsdCtx_t * p_dec,AvsdSyntax_t * syn)583 MPP_RET avsd_fill_parameters(AvsdCtx_t *p_dec, AvsdSyntax_t *syn)
584 {
585 RK_S32 i = 0;
586 PicParams_Avsd *pp = &syn->pp;
587
588 //!< sequence header
589 pp->profileId = p_dec->vsh.profile_id;
590 pp->levelId = p_dec->vsh.level_id;
591 pp->progressiveSequence = p_dec->vsh.progressive_sequence;
592 pp->horizontalSize = p_dec->vsh.horizontal_size;
593 pp->verticalSize = p_dec->vsh.vertical_size;
594 pp->chromaFormat = p_dec->vsh.chroma_format;
595 pp->aspectRatio = p_dec->vsh.aspect_ratio;
596 pp->frameRateCode = p_dec->vsh.frame_rate_code;
597 pp->bitRateValue = p_dec->vsh.bit_rate;
598 pp->lowDelay = p_dec->vsh.low_delay;
599 pp->bbvBufferSize = p_dec->vsh.bbv_buffer_size;
600
601 //!< sequence display extension header
602 pp->videoFormat = p_dec->ext.video_format;
603 pp->sampleRange = p_dec->ext.sample_range;
604 pp->colorDescription = p_dec->ext.color_description;
605 pp->colorPrimaries = p_dec->ext.color_primaries;
606 pp->transferCharacteristics = p_dec->ext.transfer_characteristics;
607 pp->matrixCoefficients = p_dec->ext.matrix_coefficients;
608 pp->displayHorizontalSize = p_dec->ext.display_horizontalSize;
609 pp->displayVerticalSize = p_dec->ext.display_verticalSize;
610
611 //!< picture header
612 pp->picCodingType = p_dec->ph.picture_coding_type;
613 pp->bbvDelay = p_dec->ph.bbv_delay;
614 pp->bbvDelayExtension = p_dec->ph.bbv_delay_extension;
615 pp->timeCodeFlag = p_dec->ph.time_code_flag;
616 pp->timeCode = p_dec->ph.time_code;
617
618 pp->pictureDistance = p_dec->ph.picture_distance;
619 pp->progressiveFrame = p_dec->ph.progressive_frame;
620 pp->pictureStructure = p_dec->ph.picture_structure;
621 pp->advancedPredModeDisable = p_dec->ph.advanced_pred_mode_disable;
622 pp->topFieldFirst = p_dec->ph.top_field_first;
623 pp->repeatFirstField = p_dec->ph.repeat_first_field;
624 pp->fixedPictureQp = p_dec->ph.fixed_picture_qp;
625 pp->pictureQp = p_dec->ph.picture_qp;
626 pp->pictureReferenceFlag = p_dec->ph.picture_reference_flag;
627 pp->skipModeFlag = p_dec->ph.skip_mode_flag;
628 pp->loopFilterDisable = p_dec->ph.loop_filter_disable;
629 pp->alphaOffset = p_dec->ph.alpha_c_offset;
630 pp->betaOffset = p_dec->ph.beta_offset;
631
632 //!< weighting quant, AVS Plus stuff
633 pp->weightingQuantFlag = p_dec->ph.weighting_quant_flag;
634 pp->chromaQuantParamDisable = p_dec->ph.chroma_quant_param_disable;
635 pp->chromaQuantParamDeltaCb = p_dec->ph.chroma_quant_param_delta_cb;
636 pp->chromaQuantParamDeltaCr = p_dec->ph.chroma_quant_param_delta_cr;
637 pp->weightingQuantParamIndex = p_dec->ph.weighting_quant_param_index;
638 pp->weightingQuantModel = p_dec->ph.weighting_quant_model;
639 for (i = 0; i < 6; i++) {
640 pp->weightingQuantParamDelta1[i] = p_dec->ph.weighting_quant_param_delta1[i];
641 pp->weightingQuantParamDelta2[i] = p_dec->ph.weighting_quant_param_delta2[i];
642 pp->weightingQuantParam[i] = p_dec->ph.weighting_quant_param[i];
643 }
644 //!< advance entropy coding
645 pp->aecEnable = p_dec->ph.aec_enable;
646
647 //!< picture enhance
648 pp->noForwardReferenceFlag = p_dec->ph.no_forward_reference_flag;
649 pp->pbFieldEnhancedFlag = p_dec->ph.pb_field_enhanced_flag;
650
651 //!< set stream offset
652 syn->bitstream_size = p_dec->cur->stream_len;
653 syn->bitstream_offset = p_dec->cur->stream_offset;
654
655 return MPP_OK;
656 }
657 /*!
658 ***********************************************************************
659 * \brief
660 * prepare function for parser
661 ***********************************************************************
662 */
avsd_parser_split(AvsdCtx_t * p,MppPacket * dst,MppPacket * src)663 MPP_RET avsd_parser_split(AvsdCtx_t *p, MppPacket *dst, MppPacket *src)
664 {
665 MPP_RET ret = MPP_NOK;
666 AVSD_PARSE_TRACE("In.\n");
667
668 RK_U8 *src_buf = (RK_U8 *)mpp_packet_get_pos(src);
669 RK_U32 src_len = (RK_U32)mpp_packet_get_length(src);
670 RK_U32 src_eos = mpp_packet_get_eos(src);
671 RK_S64 src_pts = mpp_packet_get_pts(src);
672 RK_U8 *dst_buf = (RK_U8 *)mpp_packet_get_data(dst);
673 RK_U32 dst_len = (RK_U32)mpp_packet_get_length(dst);
674 RK_U32 src_pos = 0;
675
676 // find the began of the vop
677 if (!p->vop_header_found) {
678 // add last startcode to the new frame data
679 if ((dst_len < sizeof(p->state))
680 && ((p->state & 0x00FFFFFF) == 0x000001)) {
681 dst_buf[0] = 0;
682 dst_buf[1] = 0;
683 dst_buf[2] = 1;
684 dst_len = 3;
685 }
686 while (src_pos < src_len) {
687 p->state = (p->state << 8) | src_buf[src_pos];
688 dst_buf[dst_len++] = src_buf[src_pos++];
689 if (p->state == I_PICUTRE_START_CODE ||
690 p->state == PB_PICUTRE_START_CODE) {
691 p->vop_header_found = 1;
692 mpp_packet_set_pts(dst, src_pts);
693 break;
694 }
695 }
696 }
697
698 // find the end of the vop
699 if (p->vop_header_found) {
700 while (src_pos < src_len) {
701 p->state = (p->state << 8) | src_buf[src_pos];
702 dst_buf[dst_len++] = src_buf[src_pos++];
703 if ((p->state & 0x00FFFFFF) == 0x000001) {
704 if (src_buf[src_pos] > (SLICE_MAX_START_CODE & 0xFF) &&
705 src_buf[src_pos] != (USER_DATA_CODE & 0xFF)) {
706 dst_len -= 3;
707 p->vop_header_found = 0;
708 ret = MPP_OK; // split complete
709 break;
710 }
711 }
712 }
713 }
714 // the last packet
715 if (src_eos && src_pos >= src_len) {
716 mpp_packet_set_eos(dst);
717 ret = MPP_OK;
718 }
719
720 AVSD_DBG(AVSD_DBG_INPUT, "[pkt_in] vop_header_found= %d, dst_len=%d, src_pos=%d\n",
721 p->vop_header_found, dst_len, src_pos);
722 // reset the src and dst
723 mpp_packet_set_length(dst, dst_len);
724 mpp_packet_set_pos(src, src_buf + src_pos);
725
726 AVSD_PARSE_TRACE("out.\n");
727
728 return ret;
729 }
730 /*!
731 ***********************************************************************
732 * \brief
733 * parse stream which ha function for parser
734 ***********************************************************************
735 */
avsd_parse_stream(AvsdCtx_t * p_dec,HalDecTask * task)736 MPP_RET avsd_parse_stream(AvsdCtx_t *p_dec, HalDecTask *task)
737 {
738 MPP_RET ret = MPP_ERR_UNKNOW;
739 RK_U32 startcode = 0xFF;
740 RK_U32 pic_type = 0;
741 RK_U32 got_slice = 0;
742
743 RK_U8 *data = (RK_U8 *)mpp_packet_get_data(task->input_packet);
744 RK_S32 length = (RK_S32)mpp_packet_get_length(task->input_packet);
745
746 mpp_set_bitread_ctx(p_dec->bx, data, length);
747 AVSD_DBG(AVSD_DBG_SYNTAX, "bytes_left_=%d\n", p_dec->bx->bytes_left_);
748 while (p_dec->bx->bytes_left_ && !got_slice) {
749 RK_S32 tmp = 0;
750 mpp_align_get_bits(p_dec->bx);
751 mpp_read_bits(p_dec->bx, 8, &tmp);
752 startcode = (startcode << 8) | tmp;
753 if ((startcode & 0xFFFFFF00) != 0x100)
754 continue;
755 AVSD_DBG(AVSD_DBG_SYNTAX, "startcode=%08x\n", startcode);
756
757 // when has not got sequence header, then do nothing
758 if (!p_dec->got_vsh &&
759 startcode != VIDEO_SEQUENCE_START_CODE) {
760 AVSD_DBG(AVSD_DBG_WARNNING, "when has not got sequence header, then do nothing\n");
761 continue;
762 }
763 //continue;
764 switch (startcode) {
765 case VIDEO_SEQUENCE_START_CODE:
766 ret = get_sequence_header(p_dec->bx, &p_dec->vsh);
767 if (ret == MPP_OK) {
768 p_dec->got_vsh = 1;
769 }
770 AVSD_DBG(AVSD_DBG_WARNNING, "got vsh %d\n", p_dec->got_vsh);
771 break;
772 case VIDEO_SEQUENCE_END_CODE:
773 break;
774 case USER_DATA_CODE:
775 break;
776 case VIDEO_EDIT_CODE:
777 p_dec->vec_flag = 0;
778 break;
779 case I_PICUTRE_START_CODE:
780 AVSD_DBG(AVSD_DBG_WARNNING, "got I picture start code\n");
781 if (!p_dec->got_keyframe) {
782 avsd_reset_parameters(p_dec);
783 p_dec->got_keyframe = 1;
784 }
785 ret = get_i_picture_header(p_dec->bx, &p_dec->vsh, &p_dec->ph);
786 if (ret == MPP_OK) {
787 p_dec->cur = get_one_save(p_dec, task);
788 p_dec->got_ph = 1;
789 }
790 p_dec->cur->pic_type = pic_type = I_PICTURE;
791 p_dec->vec_flag++;
792 break;
793 case EXTENSION_START_CODE:
794 ret = get_extension_header(p_dec->bx, &p_dec->ext);
795 break;
796 case PB_PICUTRE_START_CODE:
797 AVSD_DBG(AVSD_DBG_WARNNING, "got PB picture start code\n");
798 if (!p_dec->got_keyframe) {
799 avsd_reset_parameters(p_dec);
800 break;
801 }
802 ret = get_pb_picture_header(p_dec->bx, &p_dec->vsh, &p_dec->ph);
803 if (ret == MPP_OK) {
804 p_dec->cur = get_one_save(p_dec, task);
805 p_dec->got_ph = 1;
806 }
807 p_dec->cur->pic_type = pic_type = p_dec->ph.picture_coding_type;
808 p_dec->vec_flag += (p_dec->vec_flag == 1 && pic_type == P_PICTURE);
809 break;
810 default:
811 if (p_dec->cur
812 && startcode >= SLICE_MIN_START_CODE
813 && startcode <= SLICE_MAX_START_CODE) {
814 got_slice = 1;
815 p_dec->cur->stream_len = length;
816 p_dec->cur->stream_offset = p_dec->bx->used_bits / 8 - 4;
817 task->valid = p_dec->got_vsh && p_dec->got_ph;
818 AVSD_DBG(AVSD_DBG_SYNTAX, "offset=%d,got_vsh=%d, got_ph=%d, task->valid=%d\n",
819 p_dec->cur->stream_offset, p_dec->got_vsh, p_dec->got_ph, task->valid);
820 }
821
822 if (p_dec->disable_error)
823 break;
824
825 if ((pic_type == P_PICTURE && !p_dec->dpb[0]) ||
826 (pic_type == B_PICTURE && !p_dec->dpb[0]) ||
827 (pic_type == B_PICTURE && !p_dec->dpb[1] && !p_dec->vsh.low_delay) ||
828 (pic_type == P_PICTURE && p_dec->vec_flag < 1) ||
829 (pic_type == B_PICTURE && p_dec->vec_flag < 2)) {
830 AVSD_DBG(AVSD_DBG_REF, "missing refer frame.\n");
831 if (!p_dec->disable_error)
832 goto __FAILED;
833 }
834 break;
835 }
836 }
837
838 if (!task->valid)
839 goto __FAILED;
840
841 return MPP_OK;
842 __FAILED:
843 task->valid = 0;
844 reset_one_save(p_dec->cur);
845 return MPP_NOK;
846 }
847