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.

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