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.

531 lines
16KB

  1. /*
  2. * HEVC video decoder
  3. *
  4. * Copyright (C) 2012 - 2013 Guillaume Martres
  5. * Copyright (C) 2012 - 2013 Gildas Cocherel
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "internal.h"
  26. #include "thread.h"
  27. #include "hevc.h"
  28. #include "hevcdec.h"
  29. void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags)
  30. {
  31. /* frame->frame can be NULL if context init failed */
  32. if (!frame->frame || !frame->frame->buf[0])
  33. return;
  34. frame->flags &= ~flags;
  35. if (!frame->flags) {
  36. ff_thread_release_buffer(s->avctx, &frame->tf);
  37. av_buffer_unref(&frame->tab_mvf_buf);
  38. frame->tab_mvf = NULL;
  39. av_buffer_unref(&frame->rpl_buf);
  40. av_buffer_unref(&frame->rpl_tab_buf);
  41. frame->rpl_tab = NULL;
  42. frame->refPicList = NULL;
  43. frame->collocated_ref = NULL;
  44. av_buffer_unref(&frame->hwaccel_priv_buf);
  45. frame->hwaccel_picture_private = NULL;
  46. }
  47. }
  48. RefPicList *ff_hevc_get_ref_list(HEVCContext *s, HEVCFrame *ref, int x0, int y0)
  49. {
  50. int x_cb = x0 >> s->ps.sps->log2_ctb_size;
  51. int y_cb = y0 >> s->ps.sps->log2_ctb_size;
  52. int pic_width_cb = s->ps.sps->ctb_width;
  53. int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[y_cb * pic_width_cb + x_cb];
  54. return (RefPicList *)ref->rpl_tab[ctb_addr_ts];
  55. }
  56. void ff_hevc_clear_refs(HEVCContext *s)
  57. {
  58. int i;
  59. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++)
  60. ff_hevc_unref_frame(s, &s->DPB[i],
  61. HEVC_FRAME_FLAG_SHORT_REF |
  62. HEVC_FRAME_FLAG_LONG_REF);
  63. }
  64. void ff_hevc_flush_dpb(HEVCContext *s)
  65. {
  66. int i;
  67. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++)
  68. ff_hevc_unref_frame(s, &s->DPB[i], ~0);
  69. }
  70. static HEVCFrame *alloc_frame(HEVCContext *s)
  71. {
  72. int i, j, ret;
  73. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  74. HEVCFrame *frame = &s->DPB[i];
  75. if (frame->frame->buf[0])
  76. continue;
  77. ret = ff_thread_get_buffer(s->avctx, &frame->tf,
  78. AV_GET_BUFFER_FLAG_REF);
  79. if (ret < 0)
  80. return NULL;
  81. frame->rpl_buf = av_buffer_allocz(s->pkt.nb_nals * sizeof(RefPicListTab));
  82. if (!frame->rpl_buf)
  83. goto fail;
  84. frame->tab_mvf_buf = av_buffer_pool_get(s->tab_mvf_pool);
  85. if (!frame->tab_mvf_buf)
  86. goto fail;
  87. frame->tab_mvf = (MvField *)frame->tab_mvf_buf->data;
  88. frame->rpl_tab_buf = av_buffer_pool_get(s->rpl_tab_pool);
  89. if (!frame->rpl_tab_buf)
  90. goto fail;
  91. frame->rpl_tab = (RefPicListTab **)frame->rpl_tab_buf->data;
  92. frame->ctb_count = s->ps.sps->ctb_width * s->ps.sps->ctb_height;
  93. for (j = 0; j < frame->ctb_count; j++)
  94. frame->rpl_tab[j] = (RefPicListTab *)frame->rpl_buf->data;
  95. frame->frame->top_field_first = s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD;
  96. frame->frame->interlaced_frame = (s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD) || (s->sei.picture_timing.picture_struct == AV_PICTURE_STRUCTURE_BOTTOM_FIELD);
  97. if (s->avctx->hwaccel) {
  98. const AVHWAccel *hwaccel = s->avctx->hwaccel;
  99. av_assert0(!frame->hwaccel_picture_private);
  100. if (hwaccel->frame_priv_data_size) {
  101. frame->hwaccel_priv_buf = av_buffer_allocz(hwaccel->frame_priv_data_size);
  102. if (!frame->hwaccel_priv_buf)
  103. goto fail;
  104. frame->hwaccel_picture_private = frame->hwaccel_priv_buf->data;
  105. }
  106. }
  107. return frame;
  108. fail:
  109. ff_hevc_unref_frame(s, frame, ~0);
  110. return NULL;
  111. }
  112. av_log(s->avctx, AV_LOG_ERROR, "Error allocating frame, DPB full.\n");
  113. return NULL;
  114. }
  115. int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc)
  116. {
  117. HEVCFrame *ref;
  118. int i;
  119. /* check that this POC doesn't already exist */
  120. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  121. HEVCFrame *frame = &s->DPB[i];
  122. if (frame->frame->buf[0] && frame->sequence == s->seq_decode &&
  123. frame->poc == poc) {
  124. av_log(s->avctx, AV_LOG_ERROR, "Duplicate POC in a sequence: %d.\n",
  125. poc);
  126. return AVERROR_INVALIDDATA;
  127. }
  128. }
  129. ref = alloc_frame(s);
  130. if (!ref)
  131. return AVERROR(ENOMEM);
  132. *frame = ref->frame;
  133. s->ref = ref;
  134. if (s->sh.pic_output_flag)
  135. ref->flags = HEVC_FRAME_FLAG_OUTPUT | HEVC_FRAME_FLAG_SHORT_REF;
  136. else
  137. ref->flags = HEVC_FRAME_FLAG_SHORT_REF;
  138. ref->poc = poc;
  139. ref->sequence = s->seq_decode;
  140. ref->frame->crop_left = s->ps.sps->output_window.left_offset;
  141. ref->frame->crop_right = s->ps.sps->output_window.right_offset;
  142. ref->frame->crop_top = s->ps.sps->output_window.top_offset;
  143. ref->frame->crop_bottom = s->ps.sps->output_window.bottom_offset;
  144. return 0;
  145. }
  146. int ff_hevc_output_frame(HEVCContext *s, AVFrame *out, int flush)
  147. {
  148. do {
  149. int nb_output = 0;
  150. int min_poc = INT_MAX;
  151. int i, min_idx, ret;
  152. if (s->sh.no_output_of_prior_pics_flag == 1 && s->no_rasl_output_flag == 1) {
  153. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  154. HEVCFrame *frame = &s->DPB[i];
  155. if (!(frame->flags & HEVC_FRAME_FLAG_BUMPING) && frame->poc != s->poc &&
  156. frame->sequence == s->seq_output) {
  157. ff_hevc_unref_frame(s, frame, HEVC_FRAME_FLAG_OUTPUT);
  158. }
  159. }
  160. }
  161. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  162. HEVCFrame *frame = &s->DPB[i];
  163. if ((frame->flags & HEVC_FRAME_FLAG_OUTPUT) &&
  164. frame->sequence == s->seq_output) {
  165. nb_output++;
  166. if (frame->poc < min_poc || nb_output == 1) {
  167. min_poc = frame->poc;
  168. min_idx = i;
  169. }
  170. }
  171. }
  172. /* wait for more frames before output */
  173. if (!flush && s->seq_output == s->seq_decode && s->ps.sps &&
  174. nb_output <= s->ps.sps->temporal_layer[s->ps.sps->max_sub_layers - 1].num_reorder_pics)
  175. return 0;
  176. if (nb_output) {
  177. HEVCFrame *frame = &s->DPB[min_idx];
  178. ret = av_frame_ref(out, frame->frame);
  179. if (frame->flags & HEVC_FRAME_FLAG_BUMPING)
  180. ff_hevc_unref_frame(s, frame, HEVC_FRAME_FLAG_OUTPUT | HEVC_FRAME_FLAG_BUMPING);
  181. else
  182. ff_hevc_unref_frame(s, frame, HEVC_FRAME_FLAG_OUTPUT);
  183. if (ret < 0)
  184. return ret;
  185. av_log(s->avctx, AV_LOG_DEBUG,
  186. "Output frame with POC %d.\n", frame->poc);
  187. return 1;
  188. }
  189. if (s->seq_output != s->seq_decode)
  190. s->seq_output = (s->seq_output + 1) & 0xff;
  191. else
  192. break;
  193. } while (1);
  194. return 0;
  195. }
  196. void ff_hevc_bump_frame(HEVCContext *s)
  197. {
  198. int dpb = 0;
  199. int min_poc = INT_MAX;
  200. int i;
  201. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  202. HEVCFrame *frame = &s->DPB[i];
  203. if ((frame->flags) &&
  204. frame->sequence == s->seq_output &&
  205. frame->poc != s->poc) {
  206. dpb++;
  207. }
  208. }
  209. if (s->ps.sps && dpb >= s->ps.sps->temporal_layer[s->ps.sps->max_sub_layers - 1].max_dec_pic_buffering) {
  210. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  211. HEVCFrame *frame = &s->DPB[i];
  212. if ((frame->flags) &&
  213. frame->sequence == s->seq_output &&
  214. frame->poc != s->poc) {
  215. if (frame->flags == HEVC_FRAME_FLAG_OUTPUT && frame->poc < min_poc) {
  216. min_poc = frame->poc;
  217. }
  218. }
  219. }
  220. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  221. HEVCFrame *frame = &s->DPB[i];
  222. if (frame->flags & HEVC_FRAME_FLAG_OUTPUT &&
  223. frame->sequence == s->seq_output &&
  224. frame->poc <= min_poc) {
  225. frame->flags |= HEVC_FRAME_FLAG_BUMPING;
  226. }
  227. }
  228. dpb--;
  229. }
  230. }
  231. static int init_slice_rpl(HEVCContext *s)
  232. {
  233. HEVCFrame *frame = s->ref;
  234. int ctb_count = frame->ctb_count;
  235. int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_segment_addr];
  236. int i;
  237. if (s->slice_idx >= frame->rpl_buf->size / sizeof(RefPicListTab))
  238. return AVERROR_INVALIDDATA;
  239. for (i = ctb_addr_ts; i < ctb_count; i++)
  240. frame->rpl_tab[i] = (RefPicListTab *)frame->rpl_buf->data + s->slice_idx;
  241. frame->refPicList = (RefPicList *)frame->rpl_tab[ctb_addr_ts];
  242. return 0;
  243. }
  244. int ff_hevc_slice_rpl(HEVCContext *s)
  245. {
  246. SliceHeader *sh = &s->sh;
  247. uint8_t nb_list = sh->slice_type == HEVC_SLICE_B ? 2 : 1;
  248. uint8_t list_idx;
  249. int i, j, ret;
  250. ret = init_slice_rpl(s);
  251. if (ret < 0)
  252. return ret;
  253. if (!(s->rps[ST_CURR_BEF].nb_refs + s->rps[ST_CURR_AFT].nb_refs +
  254. s->rps[LT_CURR].nb_refs)) {
  255. av_log(s->avctx, AV_LOG_ERROR, "Zero refs in the frame RPS.\n");
  256. return AVERROR_INVALIDDATA;
  257. }
  258. for (list_idx = 0; list_idx < nb_list; list_idx++) {
  259. RefPicList rpl_tmp = { { 0 } };
  260. RefPicList *rpl = &s->ref->refPicList[list_idx];
  261. /* The order of the elements is
  262. * ST_CURR_BEF - ST_CURR_AFT - LT_CURR for the L0 and
  263. * ST_CURR_AFT - ST_CURR_BEF - LT_CURR for the L1 */
  264. int cand_lists[3] = { list_idx ? ST_CURR_AFT : ST_CURR_BEF,
  265. list_idx ? ST_CURR_BEF : ST_CURR_AFT,
  266. LT_CURR };
  267. /* concatenate the candidate lists for the current frame */
  268. while (rpl_tmp.nb_refs < sh->nb_refs[list_idx]) {
  269. for (i = 0; i < FF_ARRAY_ELEMS(cand_lists); i++) {
  270. RefPicList *rps = &s->rps[cand_lists[i]];
  271. for (j = 0; j < rps->nb_refs && rpl_tmp.nb_refs < HEVC_MAX_REFS; j++) {
  272. rpl_tmp.list[rpl_tmp.nb_refs] = rps->list[j];
  273. rpl_tmp.ref[rpl_tmp.nb_refs] = rps->ref[j];
  274. rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = i == 2;
  275. rpl_tmp.nb_refs++;
  276. }
  277. }
  278. }
  279. /* reorder the references if necessary */
  280. if (sh->rpl_modification_flag[list_idx]) {
  281. for (i = 0; i < sh->nb_refs[list_idx]; i++) {
  282. int idx = sh->list_entry_lx[list_idx][i];
  283. if (idx >= rpl_tmp.nb_refs) {
  284. av_log(s->avctx, AV_LOG_ERROR, "Invalid reference index.\n");
  285. return AVERROR_INVALIDDATA;
  286. }
  287. rpl->list[i] = rpl_tmp.list[idx];
  288. rpl->ref[i] = rpl_tmp.ref[idx];
  289. rpl->isLongTerm[i] = rpl_tmp.isLongTerm[idx];
  290. rpl->nb_refs++;
  291. }
  292. } else {
  293. memcpy(rpl, &rpl_tmp, sizeof(*rpl));
  294. rpl->nb_refs = FFMIN(rpl->nb_refs, sh->nb_refs[list_idx]);
  295. }
  296. if (sh->collocated_list == list_idx &&
  297. sh->collocated_ref_idx < rpl->nb_refs)
  298. s->ref->collocated_ref = rpl->ref[sh->collocated_ref_idx];
  299. }
  300. return 0;
  301. }
  302. static HEVCFrame *find_ref_idx(HEVCContext *s, int poc)
  303. {
  304. int i;
  305. int LtMask = (1 << s->ps.sps->log2_max_poc_lsb) - 1;
  306. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  307. HEVCFrame *ref = &s->DPB[i];
  308. if (ref->frame->buf[0] && (ref->sequence == s->seq_decode)) {
  309. if ((ref->poc & LtMask) == poc)
  310. return ref;
  311. }
  312. }
  313. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  314. HEVCFrame *ref = &s->DPB[i];
  315. if (ref->frame->buf[0] && ref->sequence == s->seq_decode) {
  316. if (ref->poc == poc || (ref->poc & LtMask) == poc)
  317. return ref;
  318. }
  319. }
  320. if (s->nal_unit_type != HEVC_NAL_CRA_NUT && !IS_BLA(s))
  321. av_log(s->avctx, AV_LOG_ERROR,
  322. "Could not find ref with POC %d\n", poc);
  323. return NULL;
  324. }
  325. static void mark_ref(HEVCFrame *frame, int flag)
  326. {
  327. frame->flags &= ~(HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF);
  328. frame->flags |= flag;
  329. }
  330. static HEVCFrame *generate_missing_ref(HEVCContext *s, int poc)
  331. {
  332. HEVCFrame *frame;
  333. int i, y;
  334. frame = alloc_frame(s);
  335. if (!frame)
  336. return NULL;
  337. if (!s->avctx->hwaccel) {
  338. if (!s->ps.sps->pixel_shift) {
  339. for (i = 0; frame->frame->buf[i]; i++)
  340. memset(frame->frame->buf[i]->data, 1 << (s->ps.sps->bit_depth - 1),
  341. frame->frame->buf[i]->size);
  342. } else {
  343. for (i = 0; frame->frame->data[i]; i++)
  344. for (y = 0; y < (s->ps.sps->height >> s->ps.sps->vshift[i]); y++) {
  345. uint8_t *dst = frame->frame->data[i] + y * frame->frame->linesize[i];
  346. AV_WN16(dst, 1 << (s->ps.sps->bit_depth - 1));
  347. av_memcpy_backptr(dst + 2, 2, 2*(s->ps.sps->width >> s->ps.sps->hshift[i]) - 2);
  348. }
  349. }
  350. }
  351. frame->poc = poc;
  352. frame->sequence = s->seq_decode;
  353. frame->flags = 0;
  354. if (s->threads_type == FF_THREAD_FRAME)
  355. ff_thread_report_progress(&frame->tf, INT_MAX, 0);
  356. return frame;
  357. }
  358. /* add a reference with the given poc to the list and mark it as used in DPB */
  359. static int add_candidate_ref(HEVCContext *s, RefPicList *list,
  360. int poc, int ref_flag)
  361. {
  362. HEVCFrame *ref = find_ref_idx(s, poc);
  363. if (ref == s->ref || list->nb_refs >= HEVC_MAX_REFS)
  364. return AVERROR_INVALIDDATA;
  365. if (!ref) {
  366. ref = generate_missing_ref(s, poc);
  367. if (!ref)
  368. return AVERROR(ENOMEM);
  369. }
  370. list->list[list->nb_refs] = ref->poc;
  371. list->ref[list->nb_refs] = ref;
  372. list->nb_refs++;
  373. mark_ref(ref, ref_flag);
  374. return 0;
  375. }
  376. int ff_hevc_frame_rps(HEVCContext *s)
  377. {
  378. const ShortTermRPS *short_rps = s->sh.short_term_rps;
  379. const LongTermRPS *long_rps = &s->sh.long_term_rps;
  380. RefPicList *rps = s->rps;
  381. int i, ret = 0;
  382. if (!short_rps) {
  383. rps[0].nb_refs = rps[1].nb_refs = 0;
  384. return 0;
  385. }
  386. /* clear the reference flags on all frames except the current one */
  387. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  388. HEVCFrame *frame = &s->DPB[i];
  389. if (frame == s->ref)
  390. continue;
  391. mark_ref(frame, 0);
  392. }
  393. for (i = 0; i < NB_RPS_TYPE; i++)
  394. rps[i].nb_refs = 0;
  395. /* add the short refs */
  396. for (i = 0; i < short_rps->num_delta_pocs; i++) {
  397. int poc = s->poc + short_rps->delta_poc[i];
  398. int list;
  399. if (!short_rps->used[i])
  400. list = ST_FOLL;
  401. else if (i < short_rps->num_negative_pics)
  402. list = ST_CURR_BEF;
  403. else
  404. list = ST_CURR_AFT;
  405. ret = add_candidate_ref(s, &rps[list], poc, HEVC_FRAME_FLAG_SHORT_REF);
  406. if (ret < 0)
  407. goto fail;
  408. }
  409. /* add the long refs */
  410. for (i = 0; i < long_rps->nb_refs; i++) {
  411. int poc = long_rps->poc[i];
  412. int list = long_rps->used[i] ? LT_CURR : LT_FOLL;
  413. ret = add_candidate_ref(s, &rps[list], poc, HEVC_FRAME_FLAG_LONG_REF);
  414. if (ret < 0)
  415. goto fail;
  416. }
  417. fail:
  418. /* release any frames that are now unused */
  419. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++)
  420. ff_hevc_unref_frame(s, &s->DPB[i], 0);
  421. return ret;
  422. }
  423. int ff_hevc_frame_nb_refs(const HEVCContext *s)
  424. {
  425. int ret = 0;
  426. int i;
  427. const ShortTermRPS *rps = s->sh.short_term_rps;
  428. const LongTermRPS *long_rps = &s->sh.long_term_rps;
  429. if (rps) {
  430. for (i = 0; i < rps->num_negative_pics; i++)
  431. ret += !!rps->used[i];
  432. for (; i < rps->num_delta_pocs; i++)
  433. ret += !!rps->used[i];
  434. }
  435. if (long_rps) {
  436. for (i = 0; i < long_rps->nb_refs; i++)
  437. ret += !!long_rps->used[i];
  438. }
  439. return ret;
  440. }