You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

770 lines
28KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... reference picture handling
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * H.264 / AVC / MPEG4 part10 reference picture handling.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #include "internal.h"
  27. #include "avcodec.h"
  28. #include "h264.h"
  29. #include "golomb.h"
  30. //#undef NDEBUG
  31. #include <assert.h>
  32. #define COPY_PICTURE(dst, src) \
  33. do {\
  34. *(dst) = *(src);\
  35. (dst)->f.extended_data = (dst)->f.data;\
  36. (dst)->tf.f = &(dst)->f;\
  37. } while (0)
  38. static void pic_as_field(Picture *pic, const int parity){
  39. int i;
  40. for (i = 0; i < 4; ++i) {
  41. if (parity == PICT_BOTTOM_FIELD)
  42. pic->f.data[i] += pic->f.linesize[i];
  43. pic->reference = parity;
  44. pic->f.linesize[i] *= 2;
  45. }
  46. pic->poc= pic->field_poc[parity == PICT_BOTTOM_FIELD];
  47. }
  48. static int split_field_copy(Picture *dest, Picture *src,
  49. int parity, int id_add){
  50. int match = !!(src->reference & parity);
  51. if (match) {
  52. COPY_PICTURE(dest, src);
  53. if(parity != PICT_FRAME){
  54. pic_as_field(dest, parity);
  55. dest->pic_id *= 2;
  56. dest->pic_id += id_add;
  57. }
  58. }
  59. return match;
  60. }
  61. static int build_def_list(Picture *def, Picture **in, int len, int is_long, int sel){
  62. int i[2]={0};
  63. int index=0;
  64. while(i[0]<len || i[1]<len){
  65. while (i[0] < len && !(in[ i[0] ] && (in[ i[0] ]->reference & sel)))
  66. i[0]++;
  67. while (i[1] < len && !(in[ i[1] ] && (in[ i[1] ]->reference & (sel^3))))
  68. i[1]++;
  69. if(i[0] < len){
  70. in[ i[0] ]->pic_id= is_long ? i[0] : in[ i[0] ]->frame_num;
  71. split_field_copy(&def[index++], in[ i[0]++ ], sel , 1);
  72. }
  73. if(i[1] < len){
  74. in[ i[1] ]->pic_id= is_long ? i[1] : in[ i[1] ]->frame_num;
  75. split_field_copy(&def[index++], in[ i[1]++ ], sel^3, 0);
  76. }
  77. }
  78. return index;
  79. }
  80. static int add_sorted(Picture **sorted, Picture **src, int len, int limit, int dir){
  81. int i, best_poc;
  82. int out_i= 0;
  83. for(;;){
  84. best_poc= dir ? INT_MIN : INT_MAX;
  85. for(i=0; i<len; i++){
  86. const int poc= src[i]->poc;
  87. if(((poc > limit) ^ dir) && ((poc < best_poc) ^ dir)){
  88. best_poc= poc;
  89. sorted[out_i]= src[i];
  90. }
  91. }
  92. if(best_poc == (dir ? INT_MIN : INT_MAX))
  93. break;
  94. limit= sorted[out_i++]->poc - dir;
  95. }
  96. return out_i;
  97. }
  98. int ff_h264_fill_default_ref_list(H264Context *h){
  99. int i, len;
  100. if(h->slice_type_nos==AV_PICTURE_TYPE_B){
  101. Picture *sorted[32];
  102. int cur_poc, list;
  103. int lens[2];
  104. if(FIELD_PICTURE)
  105. cur_poc= h->cur_pic_ptr->field_poc[h->picture_structure == PICT_BOTTOM_FIELD];
  106. else
  107. cur_poc= h->cur_pic_ptr->poc;
  108. for(list= 0; list<2; list++){
  109. len= add_sorted(sorted , h->short_ref, h->short_ref_count, cur_poc, 1^list);
  110. len+=add_sorted(sorted+len, h->short_ref, h->short_ref_count, cur_poc, 0^list);
  111. assert(len<=32);
  112. len= build_def_list(h->default_ref_list[list] , sorted , len, 0, h->picture_structure);
  113. len+=build_def_list(h->default_ref_list[list]+len, h->long_ref, 16 , 1, h->picture_structure);
  114. assert(len<=32);
  115. if(len < h->ref_count[list])
  116. memset(&h->default_ref_list[list][len], 0, sizeof(Picture)*(h->ref_count[list] - len));
  117. lens[list]= len;
  118. }
  119. if(lens[0] == lens[1] && lens[1] > 1){
  120. for (i = 0; h->default_ref_list[0][i].f.data[0] == h->default_ref_list[1][i].f.data[0] && i < lens[0]; i++);
  121. if (i == lens[0]) {
  122. Picture tmp;
  123. COPY_PICTURE(&tmp, &h->default_ref_list[1][0]);
  124. COPY_PICTURE(&h->default_ref_list[1][0], &h->default_ref_list[1][1]);
  125. COPY_PICTURE(&h->default_ref_list[1][1], &tmp);
  126. }
  127. }
  128. }else{
  129. len = build_def_list(h->default_ref_list[0] , h->short_ref, h->short_ref_count, 0, h->picture_structure);
  130. len+= build_def_list(h->default_ref_list[0]+len, h-> long_ref, 16 , 1, h->picture_structure);
  131. assert(len <= 32);
  132. if(len < h->ref_count[0])
  133. memset(&h->default_ref_list[0][len], 0, sizeof(Picture)*(h->ref_count[0] - len));
  134. }
  135. #ifdef TRACE
  136. for (i=0; i<h->ref_count[0]; i++) {
  137. tprintf(h->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].f.data[0]);
  138. }
  139. if(h->slice_type_nos==AV_PICTURE_TYPE_B){
  140. for (i=0; i<h->ref_count[1]; i++) {
  141. tprintf(h->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].f.data[0]);
  142. }
  143. }
  144. #endif
  145. return 0;
  146. }
  147. static void print_short_term(H264Context *h);
  148. static void print_long_term(H264Context *h);
  149. /**
  150. * Extract structure information about the picture described by pic_num in
  151. * the current decoding context (frame or field). Note that pic_num is
  152. * picture number without wrapping (so, 0<=pic_num<max_pic_num).
  153. * @param pic_num picture number for which to extract structure information
  154. * @param structure one of PICT_XXX describing structure of picture
  155. * with pic_num
  156. * @return frame number (short term) or long term index of picture
  157. * described by pic_num
  158. */
  159. static int pic_num_extract(H264Context *h, int pic_num, int *structure){
  160. *structure = h->picture_structure;
  161. if(FIELD_PICTURE){
  162. if (!(pic_num & 1))
  163. /* opposite field */
  164. *structure ^= PICT_FRAME;
  165. pic_num >>= 1;
  166. }
  167. return pic_num;
  168. }
  169. int ff_h264_decode_ref_pic_list_reordering(H264Context *h){
  170. int list, index, pic_structure, i;
  171. print_short_term(h);
  172. print_long_term(h);
  173. for(list=0; list<h->list_count; list++){
  174. for (i = 0; i < h->ref_count[list]; i++)
  175. COPY_PICTURE(&h->ref_list[list][i], &h->default_ref_list[list][i]);
  176. if(get_bits1(&h->gb)){
  177. int pred= h->curr_pic_num;
  178. for(index=0; ; index++){
  179. unsigned int reordering_of_pic_nums_idc= get_ue_golomb_31(&h->gb);
  180. unsigned int pic_id;
  181. int i;
  182. Picture *ref = NULL;
  183. if(reordering_of_pic_nums_idc==3)
  184. break;
  185. if(index >= h->ref_count[list]){
  186. av_log(h->avctx, AV_LOG_ERROR, "reference count overflow\n");
  187. return -1;
  188. }
  189. if(reordering_of_pic_nums_idc<3){
  190. if(reordering_of_pic_nums_idc<2){
  191. const unsigned int abs_diff_pic_num= get_ue_golomb(&h->gb) + 1;
  192. int frame_num;
  193. if(abs_diff_pic_num > h->max_pic_num){
  194. av_log(h->avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");
  195. return -1;
  196. }
  197. if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;
  198. else pred+= abs_diff_pic_num;
  199. pred &= h->max_pic_num - 1;
  200. frame_num = pic_num_extract(h, pred, &pic_structure);
  201. for(i= h->short_ref_count-1; i>=0; i--){
  202. ref = h->short_ref[i];
  203. assert(ref->reference);
  204. assert(!ref->long_ref);
  205. if(
  206. ref->frame_num == frame_num &&
  207. (ref->reference & pic_structure)
  208. )
  209. break;
  210. }
  211. if(i>=0)
  212. ref->pic_id= pred;
  213. }else{
  214. int long_idx;
  215. pic_id= get_ue_golomb(&h->gb); //long_term_pic_idx
  216. long_idx= pic_num_extract(h, pic_id, &pic_structure);
  217. if(long_idx>31){
  218. av_log(h->avctx, AV_LOG_ERROR, "long_term_pic_idx overflow\n");
  219. return -1;
  220. }
  221. ref = h->long_ref[long_idx];
  222. assert(!(ref && !ref->reference));
  223. if (ref && (ref->reference & pic_structure)) {
  224. ref->pic_id= pic_id;
  225. assert(ref->long_ref);
  226. i=0;
  227. }else{
  228. i=-1;
  229. }
  230. }
  231. if (i < 0) {
  232. av_log(h->avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");
  233. memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME
  234. } else {
  235. for(i=index; i+1<h->ref_count[list]; i++){
  236. if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)
  237. break;
  238. }
  239. for(; i > index; i--){
  240. COPY_PICTURE(&h->ref_list[list][i], &h->ref_list[list][i - 1]);
  241. }
  242. COPY_PICTURE(&h->ref_list[list][index], ref);
  243. if (FIELD_PICTURE){
  244. pic_as_field(&h->ref_list[list][index], pic_structure);
  245. }
  246. }
  247. }else{
  248. av_log(h->avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");
  249. return -1;
  250. }
  251. }
  252. }
  253. }
  254. for(list=0; list<h->list_count; list++){
  255. for(index= 0; index < h->ref_count[list]; index++){
  256. if (!h->ref_list[list][index].f.data[0]) {
  257. av_log(h->avctx, AV_LOG_ERROR, "Missing reference picture\n");
  258. if (h->default_ref_list[list][0].f.data[0])
  259. COPY_PICTURE(&h->ref_list[list][index], &h->default_ref_list[list][0]);
  260. else
  261. return -1;
  262. }
  263. }
  264. }
  265. return 0;
  266. }
  267. void ff_h264_fill_mbaff_ref_list(H264Context *h){
  268. int list, i, j;
  269. for(list=0; list<2; list++){ //FIXME try list_count
  270. for(i=0; i<h->ref_count[list]; i++){
  271. Picture *frame = &h->ref_list[list][i];
  272. Picture *field = &h->ref_list[list][16+2*i];
  273. COPY_PICTURE(field, frame);
  274. for(j=0; j<3; j++)
  275. field[0].f.linesize[j] <<= 1;
  276. field[0].reference = PICT_TOP_FIELD;
  277. field[0].poc= field[0].field_poc[0];
  278. COPY_PICTURE(field + 1, field);
  279. for(j=0; j<3; j++)
  280. field[1].f.data[j] += frame->f.linesize[j];
  281. field[1].reference = PICT_BOTTOM_FIELD;
  282. field[1].poc= field[1].field_poc[1];
  283. h->luma_weight[16+2*i][list][0] = h->luma_weight[16+2*i+1][list][0] = h->luma_weight[i][list][0];
  284. h->luma_weight[16+2*i][list][1] = h->luma_weight[16+2*i+1][list][1] = h->luma_weight[i][list][1];
  285. for(j=0; j<2; j++){
  286. h->chroma_weight[16+2*i][list][j][0] = h->chroma_weight[16+2*i+1][list][j][0] = h->chroma_weight[i][list][j][0];
  287. h->chroma_weight[16+2*i][list][j][1] = h->chroma_weight[16+2*i+1][list][j][1] = h->chroma_weight[i][list][j][1];
  288. }
  289. }
  290. }
  291. }
  292. /**
  293. * Mark a picture as no longer needed for reference. The refmask
  294. * argument allows unreferencing of individual fields or the whole frame.
  295. * If the picture becomes entirely unreferenced, but is being held for
  296. * display purposes, it is marked as such.
  297. * @param refmask mask of fields to unreference; the mask is bitwise
  298. * anded with the reference marking of pic
  299. * @return non-zero if pic becomes entirely unreferenced (except possibly
  300. * for display purposes) zero if one of the fields remains in
  301. * reference
  302. */
  303. static inline int unreference_pic(H264Context *h, Picture *pic, int refmask){
  304. int i;
  305. if (pic->reference &= refmask) {
  306. return 0;
  307. } else {
  308. for(i = 0; h->delayed_pic[i]; i++)
  309. if(pic == h->delayed_pic[i]){
  310. pic->reference = DELAYED_PIC_REF;
  311. break;
  312. }
  313. return 1;
  314. }
  315. }
  316. /**
  317. * Find a Picture in the short term reference list by frame number.
  318. * @param frame_num frame number to search for
  319. * @param idx the index into h->short_ref where returned picture is found
  320. * undefined if no picture found.
  321. * @return pointer to the found picture, or NULL if no pic with the provided
  322. * frame number is found
  323. */
  324. static Picture * find_short(H264Context *h, int frame_num, int *idx){
  325. int i;
  326. for(i=0; i<h->short_ref_count; i++){
  327. Picture *pic= h->short_ref[i];
  328. if(h->avctx->debug&FF_DEBUG_MMCO)
  329. av_log(h->avctx, AV_LOG_DEBUG, "%d %d %p\n", i, pic->frame_num, pic);
  330. if(pic->frame_num == frame_num) {
  331. *idx = i;
  332. return pic;
  333. }
  334. }
  335. return NULL;
  336. }
  337. /**
  338. * Remove a picture from the short term reference list by its index in
  339. * that list. This does no checking on the provided index; it is assumed
  340. * to be valid. Other list entries are shifted down.
  341. * @param i index into h->short_ref of picture to remove.
  342. */
  343. static void remove_short_at_index(H264Context *h, int i){
  344. assert(i >= 0 && i < h->short_ref_count);
  345. h->short_ref[i]= NULL;
  346. if (--h->short_ref_count)
  347. memmove(&h->short_ref[i], &h->short_ref[i+1], (h->short_ref_count - i)*sizeof(Picture*));
  348. }
  349. /**
  350. *
  351. * @return the removed picture or NULL if an error occurs
  352. */
  353. static Picture * remove_short(H264Context *h, int frame_num, int ref_mask){
  354. Picture *pic;
  355. int i;
  356. if(h->avctx->debug&FF_DEBUG_MMCO)
  357. av_log(h->avctx, AV_LOG_DEBUG, "remove short %d count %d\n", frame_num, h->short_ref_count);
  358. pic = find_short(h, frame_num, &i);
  359. if (pic){
  360. if(unreference_pic(h, pic, ref_mask))
  361. remove_short_at_index(h, i);
  362. }
  363. return pic;
  364. }
  365. /**
  366. * Remove a picture from the long term reference list by its index in
  367. * that list.
  368. * @return the removed picture or NULL if an error occurs
  369. */
  370. static Picture * remove_long(H264Context *h, int i, int ref_mask){
  371. Picture *pic;
  372. pic= h->long_ref[i];
  373. if (pic){
  374. if(unreference_pic(h, pic, ref_mask)){
  375. assert(h->long_ref[i]->long_ref == 1);
  376. h->long_ref[i]->long_ref= 0;
  377. h->long_ref[i]= NULL;
  378. h->long_ref_count--;
  379. }
  380. }
  381. return pic;
  382. }
  383. void ff_h264_remove_all_refs(H264Context *h){
  384. int i;
  385. for(i=0; i<16; i++){
  386. remove_long(h, i, 0);
  387. }
  388. assert(h->long_ref_count==0);
  389. for(i=0; i<h->short_ref_count; i++){
  390. unreference_pic(h, h->short_ref[i], 0);
  391. h->short_ref[i]= NULL;
  392. }
  393. h->short_ref_count=0;
  394. }
  395. /**
  396. * print short term list
  397. */
  398. static void print_short_term(H264Context *h) {
  399. uint32_t i;
  400. if(h->avctx->debug&FF_DEBUG_MMCO) {
  401. av_log(h->avctx, AV_LOG_DEBUG, "short term list:\n");
  402. for(i=0; i<h->short_ref_count; i++){
  403. Picture *pic= h->short_ref[i];
  404. av_log(h->avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n",
  405. i, pic->frame_num, pic->poc, pic->f.data[0]);
  406. }
  407. }
  408. }
  409. /**
  410. * print long term list
  411. */
  412. static void print_long_term(H264Context *h) {
  413. uint32_t i;
  414. if(h->avctx->debug&FF_DEBUG_MMCO) {
  415. av_log(h->avctx, AV_LOG_DEBUG, "long term list:\n");
  416. for(i = 0; i < 16; i++){
  417. Picture *pic= h->long_ref[i];
  418. if (pic) {
  419. av_log(h->avctx, AV_LOG_DEBUG, "%d fn:%d poc:%d %p\n",
  420. i, pic->frame_num, pic->poc, pic->f.data[0]);
  421. }
  422. }
  423. }
  424. }
  425. static int check_opcodes(MMCO *mmco1, MMCO *mmco2, int n_mmcos)
  426. {
  427. int i;
  428. for (i = 0; i < n_mmcos; i++) {
  429. if (mmco1[i].opcode != mmco2[i].opcode)
  430. return -1;
  431. }
  432. return 0;
  433. }
  434. int ff_generate_sliding_window_mmcos(H264Context *h, int first_slice)
  435. {
  436. MMCO mmco_temp[MAX_MMCO_COUNT], *mmco = first_slice ? h->mmco : mmco_temp;
  437. int mmco_index = 0, i;
  438. assert(h->long_ref_count + h->short_ref_count <= h->sps.ref_frame_count);
  439. if (h->short_ref_count &&
  440. h->long_ref_count + h->short_ref_count == h->sps.ref_frame_count &&
  441. !(FIELD_PICTURE && !h->first_field && h->cur_pic_ptr->reference)) {
  442. mmco[0].opcode = MMCO_SHORT2UNUSED;
  443. mmco[0].short_pic_num = h->short_ref[h->short_ref_count - 1]->frame_num;
  444. mmco_index = 1;
  445. if (FIELD_PICTURE) {
  446. mmco[0].short_pic_num *= 2;
  447. mmco[1].opcode = MMCO_SHORT2UNUSED;
  448. mmco[1].short_pic_num = mmco[0].short_pic_num + 1;
  449. mmco_index = 2;
  450. }
  451. }
  452. if (first_slice) {
  453. h->mmco_index = mmco_index;
  454. } else if (!first_slice && mmco_index >= 0 &&
  455. (mmco_index != h->mmco_index ||
  456. (i = check_opcodes(h->mmco, mmco_temp, mmco_index)))) {
  457. av_log(h->avctx, AV_LOG_ERROR,
  458. "Inconsistent MMCO state between slices [%d, %d, %d]\n",
  459. mmco_index, h->mmco_index, i);
  460. return AVERROR_INVALIDDATA;
  461. }
  462. return 0;
  463. }
  464. int ff_h264_execute_ref_pic_marking(H264Context *h, MMCO *mmco, int mmco_count){
  465. int i, av_uninit(j);
  466. int current_ref_assigned=0, err=0;
  467. Picture *av_uninit(pic);
  468. if((h->avctx->debug&FF_DEBUG_MMCO) && mmco_count==0)
  469. av_log(h->avctx, AV_LOG_DEBUG, "no mmco here\n");
  470. for(i=0; i<mmco_count; i++){
  471. int av_uninit(structure), av_uninit(frame_num);
  472. if(h->avctx->debug&FF_DEBUG_MMCO)
  473. av_log(h->avctx, AV_LOG_DEBUG, "mmco:%d %d %d\n", h->mmco[i].opcode, h->mmco[i].short_pic_num, h->mmco[i].long_arg);
  474. if( mmco[i].opcode == MMCO_SHORT2UNUSED
  475. || mmco[i].opcode == MMCO_SHORT2LONG){
  476. frame_num = pic_num_extract(h, mmco[i].short_pic_num, &structure);
  477. pic = find_short(h, frame_num, &j);
  478. if(!pic){
  479. if(mmco[i].opcode != MMCO_SHORT2LONG || !h->long_ref[mmco[i].long_arg]
  480. || h->long_ref[mmco[i].long_arg]->frame_num != frame_num) {
  481. av_log(h->avctx, AV_LOG_ERROR, "mmco: unref short failure\n");
  482. err = AVERROR_INVALIDDATA;
  483. }
  484. continue;
  485. }
  486. }
  487. switch(mmco[i].opcode){
  488. case MMCO_SHORT2UNUSED:
  489. if(h->avctx->debug&FF_DEBUG_MMCO)
  490. av_log(h->avctx, AV_LOG_DEBUG, "mmco: unref short %d count %d\n", h->mmco[i].short_pic_num, h->short_ref_count);
  491. remove_short(h, frame_num, structure ^ PICT_FRAME);
  492. break;
  493. case MMCO_SHORT2LONG:
  494. if (h->long_ref[mmco[i].long_arg] != pic)
  495. remove_long(h, mmco[i].long_arg, 0);
  496. remove_short_at_index(h, j);
  497. h->long_ref[ mmco[i].long_arg ]= pic;
  498. if (h->long_ref[ mmco[i].long_arg ]){
  499. h->long_ref[ mmco[i].long_arg ]->long_ref=1;
  500. h->long_ref_count++;
  501. }
  502. break;
  503. case MMCO_LONG2UNUSED:
  504. j = pic_num_extract(h, mmco[i].long_arg, &structure);
  505. pic = h->long_ref[j];
  506. if (pic) {
  507. remove_long(h, j, structure ^ PICT_FRAME);
  508. } else if(h->avctx->debug&FF_DEBUG_MMCO)
  509. av_log(h->avctx, AV_LOG_DEBUG, "mmco: unref long failure\n");
  510. break;
  511. case MMCO_LONG:
  512. // Comment below left from previous code as it is an interresting note.
  513. /* First field in pair is in short term list or
  514. * at a different long term index.
  515. * This is not allowed; see 7.4.3.3, notes 2 and 3.
  516. * Report the problem and keep the pair where it is,
  517. * and mark this field valid.
  518. */
  519. if (h->long_ref[mmco[i].long_arg] != h->cur_pic_ptr) {
  520. remove_long(h, mmco[i].long_arg, 0);
  521. h->long_ref[ mmco[i].long_arg ]= h->cur_pic_ptr;
  522. h->long_ref[ mmco[i].long_arg ]->long_ref=1;
  523. h->long_ref_count++;
  524. }
  525. h->cur_pic_ptr->reference |= h->picture_structure;
  526. current_ref_assigned=1;
  527. break;
  528. case MMCO_SET_MAX_LONG:
  529. assert(mmco[i].long_arg <= 16);
  530. // just remove the long term which index is greater than new max
  531. for(j = mmco[i].long_arg; j<16; j++){
  532. remove_long(h, j, 0);
  533. }
  534. break;
  535. case MMCO_RESET:
  536. while(h->short_ref_count){
  537. remove_short(h, h->short_ref[0]->frame_num, 0);
  538. }
  539. for(j = 0; j < 16; j++) {
  540. remove_long(h, j, 0);
  541. }
  542. h->frame_num=
  543. h->cur_pic_ptr->frame_num= 0;
  544. h->mmco_reset = 1;
  545. h->cur_pic_ptr->mmco_reset=1;
  546. break;
  547. default: assert(0);
  548. }
  549. }
  550. if (!current_ref_assigned) {
  551. /* Second field of complementary field pair; the first field of
  552. * which is already referenced. If short referenced, it
  553. * should be first entry in short_ref. If not, it must exist
  554. * in long_ref; trying to put it on the short list here is an
  555. * error in the encoded bit stream (ref: 7.4.3.3, NOTE 2 and 3).
  556. */
  557. if (h->short_ref_count && h->short_ref[0] == h->cur_pic_ptr) {
  558. /* Just mark the second field valid */
  559. h->cur_pic_ptr->reference = PICT_FRAME;
  560. } else if (h->cur_pic_ptr->long_ref) {
  561. av_log(h->avctx, AV_LOG_ERROR, "illegal short term reference "
  562. "assignment for second field "
  563. "in complementary field pair "
  564. "(first field is long term)\n");
  565. err = AVERROR_INVALIDDATA;
  566. } else {
  567. pic= remove_short(h, h->cur_pic_ptr->frame_num, 0);
  568. if(pic){
  569. av_log(h->avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
  570. err = AVERROR_INVALIDDATA;
  571. }
  572. if(h->short_ref_count)
  573. memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
  574. h->short_ref[0]= h->cur_pic_ptr;
  575. h->short_ref_count++;
  576. h->cur_pic_ptr->reference |= h->picture_structure;
  577. }
  578. }
  579. if (h->long_ref_count + h->short_ref_count -
  580. (h->short_ref[0] == h->cur_pic_ptr) > h->sps.ref_frame_count){
  581. /* We have too many reference frames, probably due to corrupted
  582. * stream. Need to discard one frame. Prevents overrun of the
  583. * short_ref and long_ref buffers.
  584. */
  585. av_log(h->avctx, AV_LOG_ERROR,
  586. "number of reference frames (%d+%d) exceeds max (%d; probably "
  587. "corrupt input), discarding one\n",
  588. h->long_ref_count, h->short_ref_count, h->sps.ref_frame_count);
  589. err = AVERROR_INVALIDDATA;
  590. if (h->long_ref_count && !h->short_ref_count) {
  591. for (i = 0; i < 16; ++i)
  592. if (h->long_ref[i])
  593. break;
  594. assert(i < 16);
  595. remove_long(h, i, 0);
  596. } else {
  597. pic = h->short_ref[h->short_ref_count - 1];
  598. remove_short(h, pic->frame_num, 0);
  599. }
  600. }
  601. print_short_term(h);
  602. print_long_term(h);
  603. return (h->avctx->err_recognition & AV_EF_EXPLODE) ? err : 0;
  604. }
  605. int ff_h264_decode_ref_pic_marking(H264Context *h, GetBitContext *gb,
  606. int first_slice)
  607. {
  608. int i, ret;
  609. MMCO mmco_temp[MAX_MMCO_COUNT], *mmco = first_slice ? h->mmco : mmco_temp;
  610. int mmco_index = 0;
  611. if (h->nal_unit_type == NAL_IDR_SLICE){ // FIXME fields
  612. skip_bits1(gb); // broken_link
  613. if (get_bits1(gb)){
  614. mmco[0].opcode = MMCO_LONG;
  615. mmco[0].long_arg = 0;
  616. mmco_index = 1;
  617. }
  618. } else {
  619. if (get_bits1(gb)) { // adaptive_ref_pic_marking_mode_flag
  620. for (i = 0; i < MAX_MMCO_COUNT; i++) {
  621. MMCOOpcode opcode = get_ue_golomb_31(gb);
  622. mmco[i].opcode = opcode;
  623. if (opcode == MMCO_SHORT2UNUSED || opcode == MMCO_SHORT2LONG){
  624. mmco[i].short_pic_num =
  625. (h->curr_pic_num - get_ue_golomb(gb) - 1) &
  626. (h->max_pic_num - 1);
  627. #if 0
  628. if (mmco[i].short_pic_num >= h->short_ref_count ||
  629. h->short_ref[ mmco[i].short_pic_num ] == NULL){
  630. av_log(s->avctx, AV_LOG_ERROR,
  631. "illegal short ref in memory management control "
  632. "operation %d\n", mmco);
  633. return -1;
  634. }
  635. #endif
  636. }
  637. if (opcode == MMCO_SHORT2LONG || opcode == MMCO_LONG2UNUSED ||
  638. opcode == MMCO_LONG || opcode == MMCO_SET_MAX_LONG) {
  639. unsigned int long_arg = get_ue_golomb_31(gb);
  640. if (long_arg >= 32 ||
  641. (long_arg >= 16 && !(opcode == MMCO_SET_MAX_LONG &&
  642. long_arg == 16) &&
  643. !(opcode == MMCO_LONG2UNUSED && FIELD_PICTURE))){
  644. av_log(h->avctx, AV_LOG_ERROR,
  645. "illegal long ref in memory management control "
  646. "operation %d\n", opcode);
  647. return -1;
  648. }
  649. mmco[i].long_arg = long_arg;
  650. }
  651. if (opcode > (unsigned) MMCO_LONG){
  652. av_log(h->avctx, AV_LOG_ERROR,
  653. "illegal memory management control operation %d\n",
  654. opcode);
  655. return -1;
  656. }
  657. if (opcode == MMCO_END)
  658. break;
  659. }
  660. mmco_index = i;
  661. } else {
  662. if (first_slice) {
  663. ret = ff_generate_sliding_window_mmcos(h, first_slice);
  664. if (ret < 0 && h->avctx->err_recognition & AV_EF_EXPLODE)
  665. return ret;
  666. }
  667. mmco_index = -1;
  668. }
  669. }
  670. if (first_slice && mmco_index != -1) {
  671. h->mmco_index = mmco_index;
  672. } else if (!first_slice && mmco_index >= 0 &&
  673. (mmco_index != h->mmco_index ||
  674. (i = check_opcodes(h->mmco, mmco_temp, mmco_index)))) {
  675. av_log(h->avctx, AV_LOG_ERROR,
  676. "Inconsistent MMCO state between slices [%d, %d, %d]\n",
  677. mmco_index, h->mmco_index, i);
  678. return AVERROR_INVALIDDATA;
  679. }
  680. return 0;
  681. }