comparison h264.c @ 10862:d9c084a0c22b libavcodec

Split all the reference picture handling off h264.c.
author michael
date Tue, 12 Jan 2010 20:59:00 +0000
parents 762e6bb0ba40
children 974ac220c93a
comparison
equal deleted inserted replaced
10861:61bf5a551856 10862:d9c084a0c22b
43 #endif 43 #endif
44 44
45 //#undef NDEBUG 45 //#undef NDEBUG
46 #include <assert.h> 46 #include <assert.h>
47 47
48 /**
49 * Value of Picture.reference when Picture is not a reference picture, but
50 * is held for delayed output.
51 */
52 #define DELAYED_PIC_REF 4
53
54 static VLC coeff_token_vlc[4]; 48 static VLC coeff_token_vlc[4];
55 static VLC_TYPE coeff_token_vlc_tables[520+332+280+256][2]; 49 static VLC_TYPE coeff_token_vlc_tables[520+332+280+256][2];
56 static const int coeff_token_vlc_tables_size[4]={520,332,280,256}; 50 static const int coeff_token_vlc_tables_size[4]={520,332,280,256};
57 51
58 static VLC chroma_dc_coeff_token_vlc; 52 static VLC chroma_dc_coeff_token_vlc;
75 static VLC_TYPE run7_vlc_table[96][2]; 69 static VLC_TYPE run7_vlc_table[96][2];
76 static const int run7_vlc_table_size = 96; 70 static const int run7_vlc_table_size = 96;
77 71
78 static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp); 72 static void svq3_luma_dc_dequant_idct_c(DCTELEM *block, int qp);
79 static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc); 73 static void svq3_add_idct_c(uint8_t *dst, DCTELEM *block, int stride, int qp, int dc);
80 static Picture * remove_long(H264Context *h, int i, int ref_mask);
81 74
82 static const uint8_t rem6[52]={ 75 static const uint8_t rem6[52]={
83 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 76 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
84 }; 77 };
85 78
2083 if (is_complex) 2076 if (is_complex)
2084 hl_decode_mb_complex(h); 2077 hl_decode_mb_complex(h);
2085 else hl_decode_mb_simple(h); 2078 else hl_decode_mb_simple(h);
2086 } 2079 }
2087 2080
2088 static void pic_as_field(Picture *pic, const int parity){
2089 int i;
2090 for (i = 0; i < 4; ++i) {
2091 if (parity == PICT_BOTTOM_FIELD)
2092 pic->data[i] += pic->linesize[i];
2093 pic->reference = parity;
2094 pic->linesize[i] *= 2;
2095 }
2096 pic->poc= pic->field_poc[parity == PICT_BOTTOM_FIELD];
2097 }
2098
2099 static int split_field_copy(Picture *dest, Picture *src,
2100 int parity, int id_add){
2101 int match = !!(src->reference & parity);
2102
2103 if (match) {
2104 *dest = *src;
2105 if(parity != PICT_FRAME){
2106 pic_as_field(dest, parity);
2107 dest->pic_id *= 2;
2108 dest->pic_id += id_add;
2109 }
2110 }
2111
2112 return match;
2113 }
2114
2115 static int build_def_list(Picture *def, Picture **in, int len, int is_long, int sel){
2116 int i[2]={0};
2117 int index=0;
2118
2119 while(i[0]<len || i[1]<len){
2120 while(i[0]<len && !(in[ i[0] ] && (in[ i[0] ]->reference & sel)))
2121 i[0]++;
2122 while(i[1]<len && !(in[ i[1] ] && (in[ i[1] ]->reference & (sel^3))))
2123 i[1]++;
2124 if(i[0] < len){
2125 in[ i[0] ]->pic_id= is_long ? i[0] : in[ i[0] ]->frame_num;
2126 split_field_copy(&def[index++], in[ i[0]++ ], sel , 1);
2127 }
2128 if(i[1] < len){
2129 in[ i[1] ]->pic_id= is_long ? i[1] : in[ i[1] ]->frame_num;
2130 split_field_copy(&def[index++], in[ i[1]++ ], sel^3, 0);
2131 }
2132 }
2133
2134 return index;
2135 }
2136
2137 static int add_sorted(Picture **sorted, Picture **src, int len, int limit, int dir){
2138 int i, best_poc;
2139 int out_i= 0;
2140
2141 for(;;){
2142 best_poc= dir ? INT_MIN : INT_MAX;
2143
2144 for(i=0; i<len; i++){
2145 const int poc= src[i]->poc;
2146 if(((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)){
2147 best_poc= poc;
2148 sorted[out_i]= src[i];
2149 }
2150 }
2151 if(best_poc == (dir ? INT_MIN : INT_MAX))
2152 break;
2153 limit= sorted[out_i++]->poc - dir;
2154 }
2155 return out_i;
2156 }
2157
2158 /**
2159 * fills the default_ref_list.
2160 */
2161 static int fill_default_ref_list(H264Context *h){
2162 MpegEncContext * const s = &h->s;
2163 int i, len;
2164
2165 if(h->slice_type_nos==FF_B_TYPE){
2166 Picture *sorted[32];
2167 int cur_poc, list;
2168 int lens[2];
2169
2170 if(FIELD_PICTURE)
2171 cur_poc= s->current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
2172 else
2173 cur_poc= s->current_picture_ptr->poc;
2174
2175 for(list= 0; list<2; list++){
2176 len= add_sorted(sorted , h->short_ref, h->short_ref_count, cur_poc, 1^list);
2177 len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);
2178 assert(len<=32);
2179 len= build_def_list(h->default_ref_list[list] , sorted , len, 0, s->picture_structure);
2180 len+=build_def_list(h->default_ref_list[list]+len, h->long_ref, 16 , 1, s->picture_structure);
2181 assert(len<=32);
2182
2183 if(len < h->ref_count[list])
2184 memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));
2185 lens[list]= len;
2186 }
2187
2188 if(lens[0] == lens[1] && lens[1] > 1){
2189 for(i=0; h->default_ref_list[0][i].data[0] == h->default_ref_list[1][i].data[0] && i<lens[0]; i++);
2190 if(i == lens[0])
2191 FFSWAP(Picture, h->default_ref_list[1][0], h->default_ref_list[1][1]);
2192 }
2193 }else{
2194 len = build_def_list(h->default_ref_list[0] , h->short_ref, h->short_ref_count, 0, s->picture_structure);
2195 len+= build_def_list(h->default_ref_list[0]+len, h-> long_ref, 16 , 1, s->picture_structure);
2196 assert(len <= 32);
2197 if(len < h->ref_count[0])
2198 memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));
2199 }
2200 #ifdef TRACE
2201 for (i=0; i<h->ref_count[0]; i++) {
2202 tprintf(h->s.avctx, "List0: %s fn:%d 0x%p\n", (h->default_ref_list[0][i].long_ref ? "LT" : "ST"), h->default_ref_list[0][i].pic_id, h->default_ref_list[0][i].data[0]);
2203 }
2204 if(h->slice_type_nos==FF_B_TYPE){
2205 for (i=0; i<h->ref_count[1]; i++) {
2206 tprintf(h->s.avctx, "List1: %s fn:%d 0x%p\n", (h->default_ref_list[1][i].long_ref ? "LT" : "ST"), h->default_ref_list[1][i].pic_id, h->default_ref_list[1][i].data[0]);
2207 }
2208 }
2209 #endif
2210 return 0;
2211 }
2212
2213 static void print_short_term(H264Context *h);
2214 static void print_long_term(H264Context *h);
2215
2216 /**
2217 * Extract structure information about the picture described by pic_num in
2218 * the current decoding context (frame or field). Note that pic_num is
2219 * picture number without wrapping (so, 0<=pic_num<max_pic_num).
2220 * @param pic_num picture number for which to extract structure information
2221 * @param structure one of PICT_XXX describing structure of picture
2222 * with pic_num
2223 * @return frame number (short term) or long term index of picture
2224 * described by pic_num
2225 */
2226 static int pic_num_extract(H264Context *h, int pic_num, int *structure){
2227 MpegEncContext * const s = &h->s;
2228
2229 *structure = s->picture_structure;
2230 if(FIELD_PICTURE){
2231 if (!(pic_num & 1))
2232 /* opposite field */
2233 *structure ^= PICT_FRAME;
2234 pic_num >>= 1;
2235 }
2236
2237 return pic_num;
2238 }
2239
2240 static int decode_ref_pic_list_reordering(H264Context *h){
2241 MpegEncContext * const s = &h->s;
2242 int list, index, pic_structure;
2243
2244 print_short_term(h);
2245 print_long_term(h);
2246
2247 for(list=0; list<h->list_count; list++){
2248 memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);
2249
2250 if(get_bits1(&s->gb)){
2251 int pred= h->curr_pic_num;
2252
2253 for(index=0; ; index++){
2254 unsigned int reordering_of_pic_nums_idc= get_ue_golomb_31(&s->gb);
2255 unsigned int pic_id;
2256 int i;
2257 Picture *ref = NULL;
2258
2259 if(reordering_of_pic_nums_idc==3)
2260 break;
2261
2262 if(index >= h->ref_count[list]){
2263 av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");
2264 return -1;
2265 }
2266
2267 if(reordering_of_pic_nums_idc<3){
2268 if(reordering_of_pic_nums_idc<2){
2269 const unsigned int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
2270 int frame_num;
2271
2272 if(abs_diff_pic_num > h->max_pic_num){
2273 av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
2274 return -1;
2275 }
2276
2277 if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
2278 else pred+= abs_diff_pic_num;
2279 pred &= h->max_pic_num - 1;
2280
2281 frame_num = pic_num_extract(h, pred, &pic_structure);
2282
2283 for(i= h->short_ref_count-1; i>=0; i--){
2284 ref = h->short_ref[i];
2285 assert(ref->reference);
2286 assert(!ref->long_ref);
2287 if(
2288 ref->frame_num == frame_num &&
2289 (ref->reference & pic_structure)
2290 )
2291 break;
2292 }
2293 if(i>=0)
2294 ref->pic_id= pred;
2295 }else{
2296 int long_idx;
2297 pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx
2298
2299 long_idx= pic_num_extract(h, pic_id, &pic_structure);
2300
2301 if(long_idx>31){
2302 av_log(h->s.avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
2303 return -1;
2304 }
2305 ref = h->long_ref[long_idx];
2306 assert(!(ref && !ref->reference));
2307 if(ref && (ref->reference & pic_structure)){
2308 ref->pic_id= pic_id;
2309 assert(ref->long_ref);
2310 i=0;
2311 }else{
2312 i=-1;
2313 }
2314 }
2315
2316 if (i < 0) {
2317 av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
2318 memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
2319 } else {
2320 for(i=index; i+1<h->ref_count[list]; i++){
2321 if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
2322 break;
2323 }
2324 for(; i > index; i--){
2325 h->ref_list[list][i]= h->ref_list[list][i-1];
2326 }
2327 h->ref_list[list][index]= *ref;
2328 if (FIELD_PICTURE){
2329 pic_as_field(&h->ref_list[list][index], pic_structure);
2330 }
2331 }
2332 }else{
2333 av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
2334 return -1;
2335 }
2336 }
2337 }
2338 }
2339 for(list=0; list<h->list_count; list++){
2340 for(index= 0; index < h->ref_count[list]; index++){
2341 if(!h->ref_list[list][index].data[0]){
2342 av_log(h->s.avctx, AV_LOG_ERROR, "Missing reference picture\n");
2343 if(h->default_ref_list[list][0].data[0])
2344 h->ref_list[list][index]= h->default_ref_list[list][0];
2345 else
2346 return -1;
2347 }
2348 }
2349 }
2350
2351 return 0;
2352 }
2353
2354 static void fill_mbaff_ref_list(H264Context *h){
2355 int list, i, j;
2356 for(list=0; list<2; list++){ //FIXME try list_count
2357 for(i=0; i<h->ref_count[list]; i++){
2358 Picture *frame = &h->ref_list[list][i];
2359 Picture *field = &h->ref_list[list][16+2*i];
2360 field[0] = *frame;
2361 for(j=0; j<3; j++)
2362 field[0].linesize[j] <<= 1;
2363 field[0].reference = PICT_TOP_FIELD;
2364 field[0].poc= field[0].field_poc[0];
2365 field[1] = field[0];
2366 for(j=0; j<3; j++)
2367 field[1].data[j] += frame->linesize[j];
2368 field[1].reference = PICT_BOTTOM_FIELD;
2369 field[1].poc= field[1].field_poc[1];
2370
2371 h->luma_weight[list][16+2*i] = h->luma_weight[list][16+2*i+1] = h->luma_weight[list][i];
2372 h->luma_offset[list][16+2*i] = h->luma_offset[list][16+2*i+1] = h->luma_offset[list][i];
2373 for(j=0; j<2; j++){
2374 h->chroma_weight[list][16+2*i][j] = h->chroma_weight[list][16+2*i+1][j] = h->chroma_weight[list][i][j];
2375 h->chroma_offset[list][16+2*i][j] = h->chroma_offset[list][16+2*i+1][j] = h->chroma_offset[list][i][j];
2376 }
2377 }
2378 }
2379 for(j=0; j<h->ref_count[1]; j++){
2380 for(i=0; i<h->ref_count[0]; i++)
2381 h->implicit_weight[j][16+2*i] = h->implicit_weight[j][16+2*i+1] = h->implicit_weight[j][i];
2382 memcpy(h->implicit_weight[16+2*j], h->implicit_weight[j], sizeof(*h->implicit_weight));
2383 memcpy(h->implicit_weight[16+2*j+1], h->implicit_weight[j], sizeof(*h->implicit_weight));
2384 }
2385 }
2386
2387 static int pred_weight_table(H264Context *h){ 2081 static int pred_weight_table(H264Context *h){
2388 MpegEncContext * const s = &h->s; 2082 MpegEncContext * const s = &h->s;
2389 int list, i; 2083 int list, i;
2390 int luma_def, chroma_def; 2084 int luma_def, chroma_def;
2391 2085
2484 } 2178 }
2485 } 2179 }
2486 } 2180 }
2487 2181
2488 /** 2182 /**
2489 * Mark a picture as no longer needed for reference. The refmask
2490 * argument allows unreferencing of individual fields or the whole frame.
2491 * If the picture becomes entirely unreferenced, but is being held for
2492 * display purposes, it is marked as such.
2493 * @param refmask mask of fields to unreference; the mask is bitwise
2494 * anded with the reference marking of pic
2495 * @return non-zero if pic becomes entirely unreferenced (except possibly
2496 * for display purposes) zero if one of the fields remains in
2497 * reference
2498 */
2499 static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
2500 int i;
2501 if (pic->reference &= refmask) {
2502 return 0;
2503 } else {
2504 for(i = 0; h->delayed_pic[i]; i++)
2505 if(pic == h->delayed_pic[i]){
2506 pic->reference=DELAYED_PIC_REF;
2507 break;
2508 }
2509 return 1;
2510 }
2511 }
2512
2513 /**
2514 * instantaneous decoder refresh. 2183 * instantaneous decoder refresh.
2515 */ 2184 */
2516 static void idr(H264Context *h){ 2185 static void idr(H264Context *h){
2517 int i; 2186 ff_h264_remove_all_refs(h);
2518
2519 for(i=0; i<16; i++){
2520 remove_long(h, i, 0);
2521 }
2522 assert(h->long_ref_count==0);
2523
2524 for(i=0; i<h->short_ref_count; i++){
2525 unreference_pic(h, h->short_ref[i], 0);
2526 h->short_ref[i]= NULL;
2527 }
2528 h->short_ref_count=0;
2529 h->prev_frame_num= 0; 2187 h->prev_frame_num= 0;
2530 h->prev_frame_num_offset= 0; 2188 h->prev_frame_num_offset= 0;
2531 h->prev_poc_msb= 2189 h->prev_poc_msb=
2532 h->prev_poc_lsb= 0; 2190 h->prev_poc_lsb= 0;
2533 } 2191 }
2547 if(h->s.current_picture_ptr) 2205 if(h->s.current_picture_ptr)
2548 h->s.current_picture_ptr->reference= 0; 2206 h->s.current_picture_ptr->reference= 0;
2549 h->s.first_field= 0; 2207 h->s.first_field= 0;
2550 ff_h264_reset_sei(h); 2208 ff_h264_reset_sei(h);
2551 ff_mpeg_flush(avctx); 2209 ff_mpeg_flush(avctx);
2552 }
2553
2554 /**
2555 * Find a Picture in the short term reference list by frame number.
2556 * @param frame_num frame number to search for
2557 * @param idx the index into h->short_ref where returned picture is found
2558 * undefined if no picture found.
2559 * @return pointer to the found picture, or NULL if no pic with the provided
2560 * frame number is found
2561 */
2562 static Picture * find_short(H264Context *h, int frame_num, int *idx){
2563 MpegEncContext * const s = &h->s;
2564 int i;
2565
2566 for(i=0; i<h->short_ref_count; i++){
2567 Picture *pic= h->short_ref[i];
2568 if(s->avctx->debug&FF_DEBUG_MMCO)
2569 av_log(h->s.avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
2570 if(pic->frame_num == frame_num) {
2571 *idx = i;
2572 return pic;
2573 }
2574 }
2575 return NULL;
2576 }
2577
2578 /**
2579 * Remove a picture from the short term reference list by its index in
2580 * that list. This does no checking on the provided index; it is assumed
2581 * to be valid. Other list entries are shifted down.
2582 * @param i index into h->short_ref of picture to remove.
2583 */
2584 static void remove_short_at_index(H264Context *h, int i){
2585 assert(i >= 0 && i < h->short_ref_count);
2586 h->short_ref[i]= NULL;
2587 if (--h->short_ref_count)
2588 memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
2589 }
2590
2591 /**
2592 *
2593 * @return the removed picture or NULL if an error occurs
2594 */
2595 static Picture * remove_short(H264Context *h, int frame_num, int ref_mask){
2596 MpegEncContext * const s = &h->s;
2597 Picture *pic;
2598 int i;
2599
2600 if(s->avctx->debug&FF_DEBUG_MMCO)
2601 av_log(h->s.avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
2602
2603 pic = find_short(h, frame_num, &i);
2604 if (pic){
2605 if(unreference_pic(h, pic, ref_mask))
2606 remove_short_at_index(h, i);
2607 }
2608
2609 return pic;
2610 }
2611
2612 /**
2613 * Remove a picture from the long term reference list by its index in
2614 * that list.
2615 * @return the removed picture or NULL if an error occurs
2616 */
2617 static Picture * remove_long(H264Context *h, int i, int ref_mask){
2618 Picture *pic;
2619
2620 pic= h->long_ref[i];
2621 if (pic){
2622 if(unreference_pic(h, pic, ref_mask)){
2623 assert(h->long_ref[i]->long_ref == 1);
2624 h->long_ref[i]->long_ref= 0;
2625 h->long_ref[i]= NULL;
2626 h->long_ref_count--;
2627 }
2628 }
2629
2630 return pic;
2631 }
2632
2633 /**
2634 * print short term list
2635 */
2636 static void print_short_term(H264Context *h) {
2637 uint32_t i;
2638 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
2639 av_log(h->s.avctx, AV_LOG_DEBUG, "short term list:\n");
2640 for(i=0; i<h->short_ref_count; i++){
2641 Picture *pic= h->short_ref[i];
2642 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
2643 }
2644 }
2645 }
2646
2647 /**
2648 * print long term list
2649 */
2650 static void print_long_term(H264Context *h) {
2651 uint32_t i;
2652 if(h->s.avctx->debug&FF_DEBUG_MMCO) {
2653 av_log(h->s.avctx, AV_LOG_DEBUG, "long term list:\n");
2654 for(i = 0; i < 16; i++){
2655 Picture *pic= h->long_ref[i];
2656 if (pic) {
2657 av_log(h->s.avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n", i, pic->frame_num, pic->poc, pic->data[0]);
2658 }
2659 }
2660 }
2661 }
2662
2663 /**
2664 * Executes the reference picture marking (memory management control operations).
2665 */
2666 static int execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
2667 MpegEncContext * const s = &h->s;
2668 int i, av_uninit(j);
2669 int current_ref_assigned=0;
2670 Picture *av_uninit(pic);
2671
2672 if((s->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
2673 av_log(h->s.avctx, AV_LOG_DEBUG, "no mmco here\n");
2674
2675 for(i=0; i<mmco_count; i++){
2676 int av_uninit(structure), av_uninit(frame_num);
2677 if(s->avctx->debug&FF_DEBUG_MMCO)
2678 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode, h->mmco[i].short_pic_num, h->mmco[i].long_arg);
2679
2680 if( mmco[i].opcode == MMCO_SHORT2UNUSED
2681 || mmco[i].opcode == MMCO_SHORT2LONG){
2682 frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
2683 pic = find_short(h, frame_num, &j);
2684 if(!pic){
2685 if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
2686 || h->long_ref[mmco[i].long_arg]->frame_num != frame_num)
2687 av_log(h->s.avctx, AV_LOG_ERROR, "mmco: unref short failure\n");
2688 continue;
2689 }
2690 }
2691
2692 switch(mmco[i].opcode){
2693 case MMCO_SHORT2UNUSED:
2694 if(s->avctx->debug&FF_DEBUG_MMCO)
2695 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref short %d count %d\n", h->mmco[i].short_pic_num, h->short_ref_count);
2696 remove_short(h, frame_num, structure ^ PICT_FRAME);
2697 break;
2698 case MMCO_SHORT2LONG:
2699 if (h->long_ref[mmco[i].long_arg] != pic)
2700 remove_long(h, mmco[i].long_arg, 0);
2701
2702 remove_short_at_index(h, j);
2703 h->long_ref[ mmco[i].long_arg ]= pic;
2704 if (h->long_ref[ mmco[i].long_arg ]){
2705 h->long_ref[ mmco[i].long_arg ]->long_ref=1;
2706 h->long_ref_count++;
2707 }
2708 break;
2709 case MMCO_LONG2UNUSED:
2710 j = pic_num_extract(h, mmco[i].long_arg, &structure);
2711 pic = h->long_ref[j];
2712 if (pic) {
2713 remove_long(h, j, structure ^ PICT_FRAME);
2714 } else if(s->avctx->debug&FF_DEBUG_MMCO)
2715 av_log(h->s.avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
2716 break;
2717 case MMCO_LONG:
2718 // Comment below left from previous code as it is an interresting note.
2719 /* First field in pair is in short term list or
2720 * at a different long term index.
2721 * This is not allowed; see 7.4.3.3, notes 2 and 3.
2722 * Report the problem and keep the pair where it is,
2723 * and mark this field valid.
2724 */
2725
2726 if (h->long_ref[mmco[i].long_arg] != s->current_picture_ptr) {
2727 remove_long(h, mmco[i].long_arg, 0);
2728
2729 h->long_ref[ mmco[i].long_arg ]= s->current_picture_ptr;
2730 h->long_ref[ mmco[i].long_arg ]->long_ref=1;
2731 h->long_ref_count++;
2732 }
2733
2734 s->current_picture_ptr->reference |= s->picture_structure;
2735 current_ref_assigned=1;
2736 break;
2737 case MMCO_SET_MAX_LONG:
2738 assert(mmco[i].long_arg <= 16);
2739 // just remove the long term which index is greater than new max
2740 for(j = mmco[i].long_arg; j<16; j++){
2741 remove_long(h, j, 0);
2742 }
2743 break;
2744 case MMCO_RESET:
2745 while(h->short_ref_count){
2746 remove_short(h, h->short_ref[0]->frame_num, 0);
2747 }
2748 for(j = 0; j < 16; j++) {
2749 remove_long(h, j, 0);
2750 }
2751 s->current_picture_ptr->poc=
2752 s->current_picture_ptr->field_poc[0]=
2753 s->current_picture_ptr->field_poc[1]=
2754 h->poc_lsb=
2755 h->poc_msb=
2756 h->frame_num=
2757 s->current_picture_ptr->frame_num= 0;
2758 s->current_picture_ptr->mmco_reset=1;
2759 break;
2760 default: assert(0);
2761 }
2762 }
2763
2764 if (!current_ref_assigned) {
2765 /* Second field of complementary field pair; the first field of
2766 * which is already referenced. If short referenced, it
2767 * should be first entry in short_ref. If not, it must exist
2768 * in long_ref; trying to put it on the short list here is an
2769 * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
2770 */
2771 if (h->short_ref_count && h->short_ref[0] == s->current_picture_ptr) {
2772 /* Just mark the second field valid */
2773 s->current_picture_ptr->reference = PICT_FRAME;
2774 } else if (s->current_picture_ptr->long_ref) {
2775 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term reference "
2776 "assignment for second field "
2777 "in complementary field pair "
2778 "(first field is long term)\n");
2779 } else {
2780 pic= remove_short(h, s->current_picture_ptr->frame_num, 0);
2781 if(pic){
2782 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
2783 }
2784
2785 if(h->short_ref_count)
2786 memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
2787
2788 h->short_ref[0]= s->current_picture_ptr;
2789 h->short_ref_count++;
2790 s->current_picture_ptr->reference |= s->picture_structure;
2791 }
2792 }
2793
2794 if (h->long_ref_count + h->short_ref_count > h->sps.ref_frame_count){
2795
2796 /* We have too many reference frames, probably due to corrupted
2797 * stream. Need to discard one frame. Prevents overrun of the
2798 * short_ref and long_ref buffers.
2799 */
2800 av_log(h->s.avctx, AV_LOG_ERROR,
2801 "number of reference frames exceeds max (probably "
2802 "corrupt input), discarding one\n");
2803
2804 if (h->long_ref_count && !h->short_ref_count) {
2805 for (i = 0; i < 16; ++i)
2806 if (h->long_ref[i])
2807 break;
2808
2809 assert(i < 16);
2810 remove_long(h, i, 0);
2811 } else {
2812 pic = h->short_ref[h->short_ref_count - 1];
2813 remove_short(h, pic->frame_num, 0);
2814 }
2815 }
2816
2817 print_short_term(h);
2818 print_long_term(h);
2819 return 0;
2820 }
2821
2822 static int decode_ref_pic_marking(H264Context *h, GetBitContext *gb){
2823 MpegEncContext * const s = &h->s;
2824 int i;
2825
2826 h->mmco_index= 0;
2827 if(h->nal_unit_type == NAL_IDR_SLICE){ //FIXME fields
2828 s->broken_link= get_bits1(gb) -1;
2829 if(get_bits1(gb)){
2830 h->mmco[0].opcode= MMCO_LONG;
2831 h->mmco[0].long_arg= 0;
2832 h->mmco_index= 1;
2833 }
2834 }else{
2835 if(get_bits1(gb)){ // adaptive_ref_pic_marking_mode_flag
2836 for(i= 0; i<MAX_MMCO_COUNT; i++) {
2837 MMCOOpcode opcode= get_ue_golomb_31(gb);
2838
2839 h->mmco[i].opcode= opcode;
2840 if(opcode==MMCO_SHORT2UNUSED || opcode==MMCO_SHORT2LONG){
2841 h->mmco[i].short_pic_num= (h->curr_pic_num - get_ue_golomb(gb) - 1) & (h->max_pic_num - 1);
2842 /* if(h->mmco[i].short_pic_num >= h->short_ref_count || h->short_ref[ h->mmco[i].short_pic_num ] == NULL){
2843 av_log(s->avctx, AV_LOG_ERROR, "illegal short ref in memory management control operation %d\n", mmco);
2844 return -1;
2845 }*/
2846 }
2847 if(opcode==MMCO_SHORT2LONG || opcode==MMCO_LONG2UNUSED || opcode==MMCO_LONG || opcode==MMCO_SET_MAX_LONG){
2848 unsigned int long_arg= get_ue_golomb_31(gb);
2849 if(long_arg >= 32 || (long_arg >= 16 && !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
2850 av_log(h->s.avctx, AV_LOG_ERROR, "illegal long ref in memory management control operation %d\n", opcode);
2851 return -1;
2852 }
2853 h->mmco[i].long_arg= long_arg;
2854 }
2855
2856 if(opcode > (unsigned)MMCO_LONG){
2857 av_log(h->s.avctx, AV_LOG_ERROR, "illegal memory management control operation %d\n", opcode);
2858 return -1;
2859 }
2860 if(opcode == MMCO_END)
2861 break;
2862 }
2863 h->mmco_index= i;
2864 }else{
2865 assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
2866
2867 if(h->short_ref_count && h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count &&
2868 !(FIELD_PICTURE && !s->first_field && s->current_picture_ptr->reference)) {
2869 h->mmco[0].opcode= MMCO_SHORT2UNUSED;
2870 h->mmco[0].short_pic_num= h->short_ref[ h->short_ref_count - 1 ]->frame_num;
2871 h->mmco_index= 1;
2872 if (FIELD_PICTURE) {
2873 h->mmco[0].short_pic_num *= 2;
2874 h->mmco[1].opcode= MMCO_SHORT2UNUSED;
2875 h->mmco[1].short_pic_num= h->mmco[0].short_pic_num + 1;
2876 h->mmco_index= 2;
2877 }
2878 }
2879 }
2880 }
2881
2882 return 0;
2883 } 2210 }
2884 2211
2885 static int init_poc(H264Context *h){ 2212 static int init_poc(H264Context *h){
2886 MpegEncContext * const s = &h->s; 2213 MpegEncContext * const s = &h->s;
2887 const int max_frame_num= 1<<h->sps.log2_max_frame_num; 2214 const int max_frame_num= 1<<h->sps.log2_max_frame_num;
3019 2346
3020 if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) 2347 if (CONFIG_H264_VDPAU_DECODER && s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
3021 ff_vdpau_h264_set_reference_frames(s); 2348 ff_vdpau_h264_set_reference_frames(s);
3022 2349
3023 if(!s->dropable) { 2350 if(!s->dropable) {
3024 execute_ref_pic_marking(h, h->mmco, h->mmco_index); 2351 ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
3025 h->prev_poc_msb= h->poc_msb; 2352 h->prev_poc_msb= h->poc_msb;
3026 h->prev_poc_lsb= h->poc_lsb; 2353 h->prev_poc_lsb= h->poc_lsb;
3027 } 2354 }
3028 h->prev_frame_num_offset= h->frame_num_offset; 2355 h->prev_frame_num_offset= h->frame_num_offset;
3029 h->prev_frame_num= h->frame_num; 2356 h->prev_frame_num= h->frame_num;
3268 if (ff_h264_frame_start(h) < 0) 2595 if (ff_h264_frame_start(h) < 0)
3269 return -1; 2596 return -1;
3270 h->prev_frame_num++; 2597 h->prev_frame_num++;
3271 h->prev_frame_num %= 1<<h->sps.log2_max_frame_num; 2598 h->prev_frame_num %= 1<<h->sps.log2_max_frame_num;
3272 s->current_picture_ptr->frame_num= h->prev_frame_num; 2599 s->current_picture_ptr->frame_num= h->prev_frame_num;
3273 execute_ref_pic_marking(h, NULL, 0); 2600 ff_h264_execute_ref_pic_marking(h, NULL, 0);
3274 } 2601 }
3275 2602
3276 /* See if we have a decoded first field looking for a pair... */ 2603 /* See if we have a decoded first field looking for a pair... */
3277 if (s0->first_field) { 2604 if (s0->first_field) {
3278 assert(s0->current_picture_ptr); 2605 assert(s0->current_picture_ptr);
3395 h->list_count= 1; 2722 h->list_count= 1;
3396 }else 2723 }else
3397 h->list_count= 0; 2724 h->list_count= 0;
3398 2725
3399 if(!default_ref_list_done){ 2726 if(!default_ref_list_done){
3400 fill_default_ref_list(h); 2727 ff_h264_fill_default_ref_list(h);
3401 } 2728 }
3402 2729
3403 if(h->slice_type_nos!=FF_I_TYPE && decode_ref_pic_list_reordering(h) < 0) 2730 if(h->slice_type_nos!=FF_I_TYPE && ff_h264_decode_ref_pic_list_reordering(h) < 0)
3404 return -1; 2731 return -1;
3405 2732
3406 if(h->slice_type_nos!=FF_I_TYPE){ 2733 if(h->slice_type_nos!=FF_I_TYPE){
3407 s->last_picture_ptr= &h->ref_list[0][0]; 2734 s->last_picture_ptr= &h->ref_list[0][0];
3408 ff_copy_picture(&s->last_picture, s->last_picture_ptr); 2735 ff_copy_picture(&s->last_picture, s->last_picture_ptr);
3424 h->chroma_weight_flag[i] = 0; 2751 h->chroma_weight_flag[i] = 0;
3425 } 2752 }
3426 } 2753 }
3427 2754
3428 if(h->nal_ref_idc) 2755 if(h->nal_ref_idc)
3429 decode_ref_pic_marking(h0, &s->gb); 2756 ff_h264_decode_ref_pic_marking(h0, &s->gb);
3430 2757
3431 if(FRAME_MBAFF) 2758 if(FRAME_MBAFF)
3432 fill_mbaff_ref_list(h); 2759 ff_h264_fill_mbaff_ref_list(h);
3433 2760
3434 if(h->slice_type_nos==FF_B_TYPE && !h->direct_spatial_mv_pred) 2761 if(h->slice_type_nos==FF_B_TYPE && !h->direct_spatial_mv_pred)
3435 ff_h264_direct_dist_scale_factor(h); 2762 ff_h264_direct_dist_scale_factor(h);
3436 ff_h264_direct_ref_list_init(h); 2763 ff_h264_direct_ref_list_init(h);
3437 2764