Mercurial > cgi-bin > hgwebdir.cgi > PR > Applications > VSs > VSs__H264__App
view libavcodec/h264_nal.c @ 3:0b056460c67d
changed code to use VSs
| author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
|---|---|
| date | Mon, 29 Oct 2012 16:44:27 +0100 |
| parents | |
| children |
line source
1 #include "h264_types.h"
2 #include "h264_data.h"
4 #include "golomb.h"
5 #include "h264_sei.h"
6 #include "h264_refs.h"
7 #include "h264_ps.h"
8 #include "h264_pred_mode.h"
9 #include "h264_misc.h"
11 static int ff_h264_decode_rbsp_trailing(const uint8_t *src){
12 int v= *src;
13 int r;
15 for(r=1; r<9; r++){
16 if(v&1) return r;
17 v>>=1;
18 }
19 return 0;
20 }
22 static int pred_weight_table(H264Slice *s, GetBitContext *gb){
23 int luma_def, chroma_def;
25 s->use_weight= 0;
26 s->use_weight_chroma= 0;
27 s->luma_log2_weight_denom= get_ue_golomb(gb);
28 s->chroma_log2_weight_denom= get_ue_golomb(gb);
29 luma_def = 1<<s->luma_log2_weight_denom;
30 chroma_def = 1<<s->chroma_log2_weight_denom;
32 for(int list=0; list<2; list++){
33 for(int i=0; i<s->ref_count[list]; i++){
34 int luma_weight_flag, chroma_weight_flag;
36 luma_weight_flag= get_bits1(gb);
37 if(luma_weight_flag){
38 s->luma_weight[i][list][0]= get_se_golomb(gb);
39 s->luma_weight[i][list][1]= get_se_golomb(gb);
40 if( s->luma_weight[i][list][0] != luma_def
41 || s->luma_weight[i][list][1] != 0) {
42 s->use_weight= 1;
43 }
44 }else{
45 s->luma_weight[i][list][0]= luma_def;
46 s->luma_weight[i][list][1]= 0;
47 }
49 chroma_weight_flag= get_bits1(gb);
50 if(chroma_weight_flag){
51 int j;
52 for(j=0; j<2; j++){
53 s->chroma_weight[i][list][j][0]= get_se_golomb(gb);
54 s->chroma_weight[i][list][j][1]= get_se_golomb(gb);
55 if( s->chroma_weight[i][list][j][0] != chroma_def
56 || s->chroma_weight[i][list][j][1] != 0) {
57 s->use_weight_chroma= 1;
58 }
59 }
60 }else{
61 int j;
62 for(j=0; j<2; j++){
63 s->chroma_weight[i][list][j][0]= chroma_def;
64 s->chroma_weight[i][list][j][1]= 0;
65 }
66 }
67 }
68 if(s->slice_type_nos != FF_B_TYPE) break;
69 }
70 s->use_weight= s->use_weight || s->use_weight_chroma;
71 return 0;
72 }
74 /**
75 * Initialize implicit_weight table.
76 */
77 static void implicit_weight_table(H264Slice *s){
78 int ref0, ref1, cur_poc, ref_start, ref_count0, ref_count1;
80 cur_poc = s->poc;
81 if( s->ref_count[0] == 1 && s->ref_count[1] == 1 && s->ref_list[0][0]->poc + s->ref_list[1][0]->poc == 2*cur_poc){
82 s->use_weight= 0;
83 s->use_weight_chroma= 0;
84 return;
85 }
86 ref_start= 0;
87 ref_count0= s->ref_count[0];
88 ref_count1= s->ref_count[1];
90 s->use_weight= 2;
91 s->use_weight_chroma= 2;
92 s->luma_log2_weight_denom= 5;
93 s->chroma_log2_weight_denom= 5;
95 for(ref0=ref_start; ref0 < ref_count0; ref0++){
96 int poc0 = s->ref_list[0][ref0]->poc;
97 for(ref1=ref_start; ref1 < ref_count1; ref1++){
98 int poc1 = s->ref_list[1][ref1]->poc;
99 int td = av_clip(poc1 - poc0, -128, 127);
100 int w= 32;
101 if(td){
102 int tb = av_clip(cur_poc - poc0, -128, 127);
103 int tx = (16384 + (FFABS(td) >> 1)) / td;
104 int dist_scale_factor = (tb*tx + 32) >> 8;
105 if(dist_scale_factor >= -64 && dist_scale_factor <= 128)
106 w = 64 - dist_scale_factor;
107 }
108 s->implicit_weight[ref0][ref1][0]=
109 s->implicit_weight[ref0][ref1][1]= w;
110 }
111 }
112 }
114 /**
115 * instantaneous decoder refresh.
116 */
117 static void idr(NalContext *n, H264Slice *s){
118 ff_h264_remove_all_refs(n, s);
119 n->prev_frame_num= 0;
120 n->prev_frame_num_offset= 0;
121 n->poc_offset += (n->prev_poc_msb<<16) + n->prev_poc_lsb;
122 n->prev_poc_msb=
123 n->prev_poc_lsb= 0;
124 }
126 static int init_poc(NalContext *n, H264Slice *s, GetBitContext *gb){
127 const int max_frame_num= 1<<n->sps.log2_max_frame_num;
128 int frame_poc;
130 if(n->sps.poc_type==0){
131 n->poc_lsb= get_bits(gb, n->sps.log2_max_poc_lsb);
132 }
134 if(n->sps.poc_type==1 && !n->sps.delta_pic_order_always_zero_flag){
135 n->delta_poc= get_se_golomb(gb);
136 }
138 n->frame_num_offset= n->prev_frame_num_offset;
139 if(n->frame_num < n->prev_frame_num)
140 n->frame_num_offset += max_frame_num;
142 if(n->sps.poc_type==0){
143 const int max_poc_lsb= 1<<n->sps.log2_max_poc_lsb;
145 if(n->poc_lsb < n->prev_poc_lsb && n->prev_poc_lsb - n->poc_lsb >= max_poc_lsb/2)
146 n->poc_msb = n->prev_poc_msb + max_poc_lsb;
147 else if(n->poc_lsb > n->prev_poc_lsb && n->prev_poc_lsb - n->poc_lsb < -max_poc_lsb/2)
148 n->poc_msb = n->prev_poc_msb - max_poc_lsb;
149 else
150 n->poc_msb = n->prev_poc_msb;
152 frame_poc = n->poc_msb + n->poc_lsb;
153 }else if(n->sps.poc_type==1){
154 int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
155 int i;
157 if(n->sps.poc_cycle_length != 0)
158 abs_frame_num = n->frame_num_offset + n->frame_num;
159 else
160 abs_frame_num = 0;
162 if(s->nal_ref_idc==0 && abs_frame_num > 0)
163 abs_frame_num--;
165 expected_delta_per_poc_cycle = 0;
166 for(i=0; i < n->sps.poc_cycle_length; i++)
167 expected_delta_per_poc_cycle += n->sps.offset_for_ref_frame[ i ]; //FIXME integrate during sps parse
169 if(abs_frame_num > 0){
170 int poc_cycle_cnt = (abs_frame_num - 1) / n->sps.poc_cycle_length;
171 int frame_num_in_poc_cycle = (abs_frame_num - 1) % n->sps.poc_cycle_length;
173 expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
174 for(i = 0; i <= frame_num_in_poc_cycle; i++)
175 expectedpoc = expectedpoc + n->sps.offset_for_ref_frame[ i ];
176 } else
177 expectedpoc = 0;
178 if(s->nal_ref_idc == 0)
179 expectedpoc = expectedpoc + n->sps.offset_for_non_ref_pic;
180 frame_poc = expectedpoc + n->delta_poc;
181 }else{
182 int poc= 2*(n->frame_num_offset + n->frame_num);
183 if(!s->nal_ref_idc)
184 poc--;
185 frame_poc= poc;
186 }
187 s->current_picture_info->poc= s->poc = frame_poc + n->poc_offset;
188 s->coded_pic_num = n->coded_pic_num++;
190 return 0;
191 }
193 static void ref2frame(NalContext *n, H264Slice *s){
194 for(int j=0; j<s->list_count; j++){
195 int *ref2frm= s->ref2frm[j];
197 ref2frm[0]=
198 ref2frm[1]= -1;
200 for(int i=0; i<s->ref_count[j]; i++){
201 ref2frm[i+2]= 15;
202 if(s->ref_list[j][i]->cpn >=0){
203 int k;
204 for(k=0; k<n->short_ref_count; k++){
205 if(n->short_ref[k]->cpn == s->ref_list[j][i]->cpn){
206 ref2frm[i+2]= k;
207 break;
208 }
209 }
210 }
211 }
212 }
213 }
215 /**
216 * decodes a slice header.
217 * This will also call MPV_common_init() and frame_start() as needed.
218 *
219 * @param h h264context
220 * @param h0 h264 master context (differs from 'h' when doing sliced based parallel decoding)
221 *
222 * @return 0 if okay, <0 if an error occurred, 1 if decoding must not be multithreaded
223 */
224 static int decode_slice_header(NalContext *n, H264Slice *s, GetBitContext *gb){
225 unsigned int first_mb_in_slice;
226 unsigned int pps_id;
227 int num_ref_idx_active_override_flag;
228 unsigned int slice_type, tmp;
230 first_mb_in_slice= get_ue_golomb(gb);
231 (void) first_mb_in_slice;
233 slice_type= get_ue_golomb_31(gb);
234 if(slice_type > 9){
235 av_log(AV_LOG_ERROR, "slice type too large (%d)\n", s->slice_type);
236 return -1;
237 }
238 if(slice_type > 4)
239 slice_type -= 5;
241 slice_type= golomb_to_pict_type[ slice_type ];
243 s->slice_type= slice_type;
244 s->slice_type_nos= slice_type & 3;
245 s->current_picture_info->slice_type_nos = s->slice_type_nos;
246 s->current_picture_info->reference= s->nal_ref_idc? 2:0;
247 s->key_frame = s->slice_type == FF_I_TYPE;
249 pps_id= get_ue_golomb(gb);
251 if(pps_id>=MAX_PPS_COUNT){
252 av_log(AV_LOG_ERROR, "pps_id out of range\n");
253 return -1;
254 }
255 if(!n->pps_buffers[pps_id]) {
256 av_log(AV_LOG_ERROR, "non-existing PPS %u referenced\n", pps_id);
257 return -1;
258 }
259 s->pps= *n->pps_buffers[pps_id];
261 if(!n->sps_buffers[s->pps.sps_id]) {
262 av_log(AV_LOG_ERROR, "non-existing SPS %u referenced\n", s->pps.sps_id);
263 return -1;
264 }
265 n->sps = *n->sps_buffers[s->pps.sps_id];
267 n->mb_width= n->sps.mb_width;
268 n->mb_height= n->sps.mb_height;
270 int chroma444 = (n->sps.chroma_format_idc == 3);
271 n->width = 16*n->mb_width - (2>>chroma444)*FFMIN(n->sps.crop_right, (8<<chroma444)-1);
272 if(n->sps.frame_mbs_only_flag)
273 n->height= 16*n->mb_height - (2>>chroma444)*FFMIN(n->sps.crop_bottom, (8<<chroma444)-1);
274 else
275 n->height= 16*n->mb_height - (4>>chroma444)*FFMIN(n->sps.crop_bottom, (8<<chroma444)-1);
277 s->direct_8x8_inference_flag = n->sps.direct_8x8_inference_flag;
278 s->transform_bypass = n->sps.transform_bypass;
280 n->frame_num= get_bits(gb, n->sps.log2_max_frame_num);
281 if(n->frame_num != n->prev_frame_num && n->frame_num != (n->prev_frame_num+1)%(1<<n->sps.log2_max_frame_num)){
282 av_log(AV_LOG_ERROR, "unexpected frame_num \n");
283 }
285 s->current_picture_info->frame_num= n->frame_num; //FIXME frame_num cleanup
286 n->max_pic_num= 1<< n->sps.log2_max_frame_num;
288 if(s->nal_unit_type == NAL_IDR_SLICE){
289 get_ue_golomb(gb); /* idr_pic_id */
290 }
292 init_poc(n, s, gb);
294 if(s->pps.redundant_pic_cnt_present){
295 n->redundant_pic_count= get_ue_golomb(gb);
296 }
298 //set defaults, might be overridden a few lines later
299 s->ref_count[0]= s->pps.ref_count[0];
300 s->ref_count[1]= s->pps.ref_count[1];
302 if(s->slice_type_nos != FF_I_TYPE){
303 if(s->slice_type_nos == FF_B_TYPE){
304 s->direct_spatial_mv_pred= get_bits1(gb);
305 }
306 num_ref_idx_active_override_flag= get_bits1(gb);
308 if(num_ref_idx_active_override_flag){
309 s->ref_count[0]= get_ue_golomb(gb) + 1;
310 if(s->slice_type_nos==FF_B_TYPE)
311 s->ref_count[1]= get_ue_golomb(gb) + 1;
313 if(s->ref_count[0]-1 > 32-1 || s->ref_count[1]-1 > 32-1){
314 av_log(AV_LOG_ERROR, "reference overflow\n");
315 s->ref_count[0]= s->ref_count[1]= 1;
316 return -1;
317 }
318 }
319 if(s->slice_type_nos == FF_B_TYPE)
320 s->list_count= 2;
321 else
322 s->list_count= 1;
323 }else
324 s->list_count= 0;
327 if(s->slice_type_nos!=FF_I_TYPE){
328 ff_h264_fill_default_ref_list(n, s);
329 ff_h264_decode_ref_pic_list_reordering(n, s, gb);
330 ref2frame(n, s);
332 for(int i=0; i<2; i++){
333 for(int j=0; j<s->ref_count[i]; j++){
334 if (s->ref_list[i][j]==NULL || s->ref_list[i][j]->reference < 2) // Don't know why sometimes the ref_count=1 while there are no references
335 s->ref_list_cpn[i][j] = -1;
336 else
337 s->ref_list_cpn[i][j] = s->ref_list[i][j]->cpn;
338 }
339 }
340 }
342 if( (s->pps.weighted_pred && s->slice_type_nos == FF_P_TYPE )
343 || (s->pps.weighted_bipred_idc==1 && s->slice_type_nos== FF_B_TYPE ) ){
344 pred_weight_table(s, gb);
345 }
346 else if(s->pps.weighted_bipred_idc==2 && s->slice_type_nos== FF_B_TYPE){
347 implicit_weight_table( s);
348 }else {
349 s->use_weight = 0;
350 }
352 if(s->nal_ref_idc){
353 ff_h264_ref_pic_marking(n, s, gb);
354 n->prev_poc_msb= n->poc_msb;
355 n->prev_poc_lsb= n->poc_lsb;
356 }
358 n->prev_frame_num_offset= n->frame_num_offset;
359 n->prev_frame_num= n->frame_num;
361 if(s->slice_type_nos != FF_B_TYPE){
362 s->ip_id= n->ip_id++;
363 }
365 if(s->slice_type_nos==FF_B_TYPE && !s->direct_spatial_mv_pred){
366 ff_h264_direct_dist_scale_factor(s);
367 }
368 ff_h264_direct_ref_list_init(s);
371 if( s->slice_type_nos != FF_I_TYPE && s->pps.cabac ){
372 tmp = get_ue_golomb_31(gb);
373 if(tmp > 2){
374 av_log(AV_LOG_ERROR, "cabac_init_idc overflow\n");
375 return -1;
376 }
377 s->cabac_init_idc= tmp;
378 }
380 tmp = s->pps.init_qp + get_se_golomb(gb);
381 if(tmp>51){
382 av_log(AV_LOG_ERROR, "QP %u out of range\n", tmp);
383 return -1;
384 }
385 s->qscale= tmp;
387 //FIXME qscale / qp ... stuff
388 if(s->slice_type == FF_SP_TYPE){
389 get_bits1(gb); /* sp_for_switch_flag */
390 }
391 if(s->slice_type==FF_SP_TYPE || s->slice_type == FF_SI_TYPE){
392 get_se_golomb(gb); /* slice_qs_delta */
393 }
395 s->slice_alpha_c0_offset = 52;
396 s->slice_beta_offset = 52;
397 if( s->pps.deblocking_filter_parameters_present ) {
398 tmp= get_ue_golomb_31(gb);
399 if(tmp > 1){
400 av_log(AV_LOG_ERROR, "deblocking_filter_idc %u out of range\n", tmp);
401 return -1;
402 }
404 if(tmp < 2)
405 tmp^= 1; // 1<->0
407 if( tmp ) {
408 s->slice_alpha_c0_offset += get_se_golomb(gb) << 1;
409 s->slice_beta_offset += get_se_golomb(gb) << 1;
410 if( (unsigned) s->slice_alpha_c0_offset > 104U
411 ||(unsigned) s->slice_beta_offset > 104U){
412 av_log(AV_LOG_ERROR, "deblocking filter parameters %d %d out of range\n", s->slice_alpha_c0_offset, s->slice_beta_offset);
413 return -1;
414 }
415 }
416 }
418 s->qp_thresh= 15 + 52 - FFMIN(s->slice_alpha_c0_offset, s->slice_beta_offset) - FFMAX3(0, s->pps.chroma_qp_index_offset[0], s->pps.chroma_qp_index_offset[1]);
420 return 0;
421 }
423 PictureInfo *get_pib_entry(NalContext *nc, int coded_pic_num){
424 PictureInfo *pic = NULL;
426 for(int i=0; i<MAX_REF_PIC_COUNT+1; i++){
427 if(nc->picture[i].reference==0){
428 pic= &nc->picture[i];
429 break;
430 }
431 }
432 pic->cpn = coded_pic_num;
434 return pic;
435 }
437 int decode_nal_units(NalContext *n, H264Slice *s, GetBitContext *gb1){
438 GetBitContext *gb = gb1;
439 uint8_t *buf = gb1->raw;
440 int buf_size = gb1->buf_size;
441 int next_avc = buf_size;
442 int buf_index=0;
443 uint8_t *dst=NULL;
444 // gb->raw = gb1->raw;
445 // gb->rbsp = NULL;
446 s->release_cnt=0;
447 ff_h264_reset_sei(n);
449 s->current_picture_info = get_pib_entry(n, n->coded_pic_num);
451 for(;;){
452 int consumed;
453 int dst_length;
454 int bit_length;
455 const uint8_t *ptr;
456 int err;
458 if (buf_index >= buf_size){
459 break;
460 } else {
461 // start code prefix search
462 for(; buf_index + 3 < buf_size; buf_index++){
463 // This should always succeed in the first iteration.
464 if(buf[buf_index] == 0 && buf[buf_index+1] == 0 && buf[buf_index+2] == 1)
465 break;
466 }
467 if(buf_index+3 >= buf_size) break;
468 buf_index+=3;
469 }
471 {
472 int length = next_avc - buf_index;
473 int i, si, di;
474 uint8_t *src= buf+buf_index;
475 // src[0]&0x80; //forbidden bit
476 s->nal_ref_idc= src[0]>>5;
477 s->nal_unit_type= src[0]&0x1F;
479 src++; length--;
481 for(i=0; i+1<length; i+=2){
482 if(src[i]) continue;
483 if(i>0 && src[i-1]==0) i--;
484 if(i+2<length && src[i+1]==0 && src[i+2]<=3){
485 if(src[i+2]!=3){
486 /* startcode, so we must be past the end */
487 length=i;
488 }
489 break;
490 }
491 }
493 if(i>=length-1){ //no escaped 0
494 dst_length= length;
495 consumed= length+1; //+1 for the header
496 ptr=src;
497 }else{
498 av_fast_malloc(&gb->rbsp, &gb->rbsp_size, length+FF_INPUT_BUFFER_PADDING_SIZE);
499 dst = gb->rbsp;
500 // if (dst){
501 // av_free(dst);
502 // }
503 // dst = av_malloc(length+FF_INPUT_BUFFER_PADDING_SIZE);
505 if (dst == NULL){
506 return -1;
507 }
509 //printf("decoding esc\n");
510 memcpy(dst, src, i);
511 si=di=i;
512 while(si+2<length){
513 //remove escapes (very rare 1:2^22)
514 if(src[si+2]>3){
515 dst[di++]= src[si++];
516 dst[di++]= src[si++];
517 }else if(src[si]==0 && src[si+1]==0){
518 if(src[si+2]==3){ //escape
519 dst[di++]= 0;
520 dst[di++]= 0;
521 si+=3;
522 continue;
523 }else //next start code
524 goto nsc;
525 }
527 dst[di++]= src[si++];
528 }
529 while(si<length)
530 dst[di++]= src[si++];
531 nsc:
533 memset(dst+di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
535 dst_length= di;
536 consumed= si + 1;//+1 for the header
537 //FIXME store exact number of bits in the getbitcontext (it is needed for decoding)
538 ptr=dst;
539 // gb->rbsp=ptr;
540 }
541 }
542 if (ptr==NULL || dst_length < 0){
543 return -1;
544 }
546 //error prevention, should not touch dst_length
547 while(ptr[dst_length - 1] == 0 && dst_length > 0)
548 dst_length--;
550 bit_length= !dst_length ? 0 : (8*dst_length - ff_h264_decode_rbsp_trailing(ptr + dst_length - 1));
551 buf_index += consumed;
553 err = 0;
554 init_get_bits(gb, ptr, bit_length);
555 switch(s->nal_unit_type){
556 case NAL_IDR_SLICE:
557 idr(n, s); //FIXME ensure we don't loose some frames if there is reordering
558 case NAL_SLICE:
559 if((err = decode_slice_header(n, s, gb)))
560 break;
561 s->key_frame |= (s->nal_unit_type == NAL_IDR_SLICE) || (n->sei_recovery_frame_cnt >= 0);
562 break;
563 case NAL_DPA:
564 case NAL_DPB:
565 case NAL_DPC:
566 av_log(AV_LOG_ERROR,"no slices/data partitioning support\n");
567 break;
568 case NAL_SEI:
569 ff_h264_decode_sei(n, gb);
570 break;
571 case NAL_SPS:
572 ff_h264_decode_seq_parameter_set(n, gb);
573 break;
574 case NAL_PPS:
575 ff_h264_decode_picture_parameter_set(n, gb, bit_length);
576 break;
577 case NAL_AUD:
578 case NAL_END_SEQUENCE:
579 case NAL_END_STREAM:
580 case NAL_FILLER_DATA:
581 case NAL_SPS_EXT:
582 case NAL_AUXILIARY_SLICE:
583 break;
584 default:
585 av_log(AV_LOG_ERROR, "Unknown NAL code: %d (%d bits)\n", s->nal_unit_type, bit_length);
586 }
587 if (err < 0)
588 av_log(AV_LOG_ERROR, "decode_slice_header error\n");
590 }
592 return buf_index;
593 }
595 NalContext *get_nal_context(int width, int height){
596 const int mb_height = (height + 15) / 16;
597 const int mb_width = (width + 15) / 16;
598 const int mb_stride = ((mb_width+1)/16 + 1) *16; //align mb_stride to 16
600 NalContext *nc = av_mallocz(sizeof(NalContext));
601 nc->width = width;
602 nc->height = height;
603 nc->mb_height = mb_height;
604 nc->mb_width = mb_width;
605 nc->b4_stride = mb_width*4 + 1;
606 nc->mb_stride = mb_stride;
607 nc->outputed_poc = INT_MIN;
609 for(int i=0; i<16; i++){
610 nc->picture[i].cpn =-1;
611 }
613 return nc;
614 }
616 void free_nal_context(NalContext *nc){
617 for(int i = 0; i < MAX_SPS_COUNT; i++){
618 if (nc->sps_buffers[i]){
619 av_free( nc->sps_buffers[i]);
620 }
621 }
622 for(int i = 0; i < MAX_PPS_COUNT; i++){
623 if (nc->pps_buffers[i]){
624 av_free( nc->pps_buffers[i]);
625 }
626 }
627 av_free(nc);
628 }
