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.

562 lines
17KB

  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->window = s->ps.sps->output_window;
  141. return 0;
  142. }
  143. int ff_hevc_output_frame(HEVCContext *s, AVFrame *out, int flush)
  144. {
  145. do {
  146. int nb_output = 0;
  147. int min_poc = INT_MAX;
  148. int i, min_idx, ret;
  149. if (s->sh.no_output_of_prior_pics_flag == 1 && s->no_rasl_output_flag == 1) {
  150. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  151. HEVCFrame *frame = &s->DPB[i];
  152. if (!(frame->flags & HEVC_FRAME_FLAG_BUMPING) && frame->poc != s->poc &&
  153. frame->sequence == s->seq_output) {
  154. ff_hevc_unref_frame(s, frame, HEVC_FRAME_FLAG_OUTPUT);
  155. }
  156. }
  157. }
  158. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  159. HEVCFrame *frame = &s->DPB[i];
  160. if ((frame->flags & HEVC_FRAME_FLAG_OUTPUT) &&
  161. frame->sequence == s->seq_output) {
  162. nb_output++;
  163. if (frame->poc < min_poc || nb_output == 1) {
  164. min_poc = frame->poc;
  165. min_idx = i;
  166. }
  167. }
  168. }
  169. /* wait for more frames before output */
  170. if (!flush && s->seq_output == s->seq_decode && s->ps.sps &&
  171. nb_output <= s->ps.sps->temporal_layer[s->ps.sps->max_sub_layers - 1].num_reorder_pics)
  172. return 0;
  173. if (nb_output) {
  174. HEVCFrame *frame = &s->DPB[min_idx];
  175. AVFrame *dst = out;
  176. AVFrame *src = frame->frame;
  177. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(src->format);
  178. int pixel_shift = !!(desc->comp[0].depth > 8);
  179. ret = av_frame_ref(out, src);
  180. if (frame->flags & HEVC_FRAME_FLAG_BUMPING)
  181. ff_hevc_unref_frame(s, frame, HEVC_FRAME_FLAG_OUTPUT | HEVC_FRAME_FLAG_BUMPING);
  182. else
  183. ff_hevc_unref_frame(s, frame, HEVC_FRAME_FLAG_OUTPUT);
  184. if (ret < 0)
  185. return ret;
  186. for (i = 0; i < 3; i++) {
  187. int hshift = (i > 0) ? desc->log2_chroma_w : 0;
  188. int vshift = (i > 0) ? desc->log2_chroma_h : 0;
  189. int off = ((frame->window.left_offset >> hshift) << pixel_shift) +
  190. (frame->window.top_offset >> vshift) * dst->linesize[i];
  191. dst->data[i] += off;
  192. }
  193. av_log(s->avctx, AV_LOG_DEBUG,
  194. "Output frame with POC %d.\n", frame->poc);
  195. return 1;
  196. }
  197. if (s->seq_output != s->seq_decode)
  198. s->seq_output = (s->seq_output + 1) & 0xff;
  199. else
  200. break;
  201. } while (1);
  202. return 0;
  203. }
  204. void ff_hevc_bump_frame(HEVCContext *s)
  205. {
  206. int dpb = 0;
  207. int min_poc = INT_MAX;
  208. int i;
  209. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  210. HEVCFrame *frame = &s->DPB[i];
  211. if ((frame->flags) &&
  212. frame->sequence == s->seq_output &&
  213. frame->poc != s->poc) {
  214. dpb++;
  215. }
  216. }
  217. if (s->ps.sps && dpb >= s->ps.sps->temporal_layer[s->ps.sps->max_sub_layers - 1].max_dec_pic_buffering) {
  218. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  219. HEVCFrame *frame = &s->DPB[i];
  220. if ((frame->flags) &&
  221. frame->sequence == s->seq_output &&
  222. frame->poc != s->poc) {
  223. if (frame->flags == HEVC_FRAME_FLAG_OUTPUT && frame->poc < min_poc) {
  224. min_poc = frame->poc;
  225. }
  226. }
  227. }
  228. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  229. HEVCFrame *frame = &s->DPB[i];
  230. if (frame->flags & HEVC_FRAME_FLAG_OUTPUT &&
  231. frame->sequence == s->seq_output &&
  232. frame->poc <= min_poc) {
  233. frame->flags |= HEVC_FRAME_FLAG_BUMPING;
  234. }
  235. }
  236. dpb--;
  237. }
  238. }
  239. static int init_slice_rpl(HEVCContext *s)
  240. {
  241. HEVCFrame *frame = s->ref;
  242. int ctb_count = frame->ctb_count;
  243. int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_segment_addr];
  244. int i;
  245. if (s->slice_idx >= frame->rpl_buf->size / sizeof(RefPicListTab))
  246. return AVERROR_INVALIDDATA;
  247. for (i = ctb_addr_ts; i < ctb_count; i++)
  248. frame->rpl_tab[i] = (RefPicListTab *)frame->rpl_buf->data + s->slice_idx;
  249. frame->refPicList = (RefPicList *)frame->rpl_tab[ctb_addr_ts];
  250. return 0;
  251. }
  252. int ff_hevc_slice_rpl(HEVCContext *s)
  253. {
  254. SliceHeader *sh = &s->sh;
  255. uint8_t nb_list = sh->slice_type == HEVC_SLICE_B ? 2 : 1;
  256. uint8_t list_idx;
  257. int i, j, ret;
  258. ret = init_slice_rpl(s);
  259. if (ret < 0)
  260. return ret;
  261. if (!(s->rps[ST_CURR_BEF].nb_refs + s->rps[ST_CURR_AFT].nb_refs +
  262. s->rps[LT_CURR].nb_refs)) {
  263. av_log(s->avctx, AV_LOG_ERROR, "Zero refs in the frame RPS.\n");
  264. return AVERROR_INVALIDDATA;
  265. }
  266. for (list_idx = 0; list_idx < nb_list; list_idx++) {
  267. RefPicList rpl_tmp = { { 0 } };
  268. RefPicList *rpl = &s->ref->refPicList[list_idx];
  269. /* The order of the elements is
  270. * ST_CURR_BEF - ST_CURR_AFT - LT_CURR for the L0 and
  271. * ST_CURR_AFT - ST_CURR_BEF - LT_CURR for the L1 */
  272. int cand_lists[3] = { list_idx ? ST_CURR_AFT : ST_CURR_BEF,
  273. list_idx ? ST_CURR_BEF : ST_CURR_AFT,
  274. LT_CURR };
  275. /* concatenate the candidate lists for the current frame */
  276. while (rpl_tmp.nb_refs < sh->nb_refs[list_idx]) {
  277. for (i = 0; i < FF_ARRAY_ELEMS(cand_lists); i++) {
  278. RefPicList *rps = &s->rps[cand_lists[i]];
  279. for (j = 0; j < rps->nb_refs && rpl_tmp.nb_refs < HEVC_MAX_REFS; j++) {
  280. rpl_tmp.list[rpl_tmp.nb_refs] = rps->list[j];
  281. rpl_tmp.ref[rpl_tmp.nb_refs] = rps->ref[j];
  282. rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = i == 2;
  283. rpl_tmp.nb_refs++;
  284. }
  285. }
  286. }
  287. /* reorder the references if necessary */
  288. if (sh->rpl_modification_flag[list_idx]) {
  289. for (i = 0; i < sh->nb_refs[list_idx]; i++) {
  290. int idx = sh->list_entry_lx[list_idx][i];
  291. if (idx >= rpl_tmp.nb_refs) {
  292. av_log(s->avctx, AV_LOG_ERROR, "Invalid reference index.\n");
  293. return AVERROR_INVALIDDATA;
  294. }
  295. rpl->list[i] = rpl_tmp.list[idx];
  296. rpl->ref[i] = rpl_tmp.ref[idx];
  297. rpl->isLongTerm[i] = rpl_tmp.isLongTerm[idx];
  298. rpl->nb_refs++;
  299. }
  300. } else {
  301. memcpy(rpl, &rpl_tmp, sizeof(*rpl));
  302. rpl->nb_refs = FFMIN(rpl->nb_refs, sh->nb_refs[list_idx]);
  303. }
  304. if (sh->collocated_list == list_idx &&
  305. sh->collocated_ref_idx < rpl->nb_refs)
  306. s->ref->collocated_ref = rpl->ref[sh->collocated_ref_idx];
  307. }
  308. return 0;
  309. }
  310. static HEVCFrame *find_ref_idx(HEVCContext *s, int poc)
  311. {
  312. int i;
  313. int LtMask = (1 << s->ps.sps->log2_max_poc_lsb) - 1;
  314. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  315. HEVCFrame *ref = &s->DPB[i];
  316. if (ref->frame->buf[0] && (ref->sequence == s->seq_decode)) {
  317. if ((ref->poc & LtMask) == poc)
  318. return ref;
  319. }
  320. }
  321. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  322. HEVCFrame *ref = &s->DPB[i];
  323. if (ref->frame->buf[0] && ref->sequence == s->seq_decode) {
  324. if (ref->poc == poc || (ref->poc & LtMask) == poc)
  325. return ref;
  326. }
  327. }
  328. if (s->nal_unit_type != HEVC_NAL_CRA_NUT && !IS_BLA(s))
  329. av_log(s->avctx, AV_LOG_ERROR,
  330. "Could not find ref with POC %d\n", poc);
  331. return NULL;
  332. }
  333. static void mark_ref(HEVCFrame *frame, int flag)
  334. {
  335. frame->flags &= ~(HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF);
  336. frame->flags |= flag;
  337. }
  338. static HEVCFrame *generate_missing_ref(HEVCContext *s, int poc)
  339. {
  340. HEVCFrame *frame;
  341. int i, x, y;
  342. frame = alloc_frame(s);
  343. if (!frame)
  344. return NULL;
  345. if (!s->avctx->hwaccel) {
  346. if (!s->ps.sps->pixel_shift) {
  347. for (i = 0; frame->frame->buf[i]; i++)
  348. memset(frame->frame->buf[i]->data, 1 << (s->ps.sps->bit_depth - 1),
  349. frame->frame->buf[i]->size);
  350. } else {
  351. for (i = 0; frame->frame->data[i]; i++)
  352. for (y = 0; y < (s->ps.sps->height >> s->ps.sps->vshift[i]); y++)
  353. for (x = 0; x < (s->ps.sps->width >> s->ps.sps->hshift[i]); x++) {
  354. AV_WN16(frame->frame->data[i] + y * frame->frame->linesize[i] + 2 * x,
  355. 1 << (s->ps.sps->bit_depth - 1));
  356. }
  357. }
  358. }
  359. frame->poc = poc;
  360. frame->sequence = s->seq_decode;
  361. frame->flags = 0;
  362. if (s->threads_type == FF_THREAD_FRAME)
  363. ff_thread_report_progress(&frame->tf, INT_MAX, 0);
  364. return frame;
  365. }
  366. /* add a reference with the given poc to the list and mark it as used in DPB */
  367. static int add_candidate_ref(HEVCContext *s, RefPicList *list,
  368. int poc, int ref_flag)
  369. {
  370. HEVCFrame *ref = find_ref_idx(s, poc);
  371. if (ref == s->ref)
  372. return AVERROR_INVALIDDATA;
  373. if (!ref) {
  374. ref = generate_missing_ref(s, poc);
  375. if (!ref)
  376. return AVERROR(ENOMEM);
  377. }
  378. list->list[list->nb_refs] = ref->poc;
  379. list->ref[list->nb_refs] = ref;
  380. list->nb_refs++;
  381. mark_ref(ref, ref_flag);
  382. return 0;
  383. }
  384. int ff_hevc_frame_rps(HEVCContext *s)
  385. {
  386. const ShortTermRPS *short_rps = s->sh.short_term_rps;
  387. const LongTermRPS *long_rps = &s->sh.long_term_rps;
  388. RefPicList *rps = s->rps;
  389. int i, ret = 0;
  390. if (!short_rps) {
  391. rps[0].nb_refs = rps[1].nb_refs = 0;
  392. return 0;
  393. }
  394. /* clear the reference flags on all frames except the current one */
  395. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  396. HEVCFrame *frame = &s->DPB[i];
  397. if (frame == s->ref)
  398. continue;
  399. mark_ref(frame, 0);
  400. }
  401. for (i = 0; i < NB_RPS_TYPE; i++)
  402. rps[i].nb_refs = 0;
  403. /* add the short refs */
  404. for (i = 0; i < short_rps->num_delta_pocs; i++) {
  405. int poc = s->poc + short_rps->delta_poc[i];
  406. int list;
  407. if (!short_rps->used[i])
  408. list = ST_FOLL;
  409. else if (i < short_rps->num_negative_pics)
  410. list = ST_CURR_BEF;
  411. else
  412. list = ST_CURR_AFT;
  413. ret = add_candidate_ref(s, &rps[list], poc, HEVC_FRAME_FLAG_SHORT_REF);
  414. if (ret < 0)
  415. goto fail;
  416. }
  417. /* add the long refs */
  418. for (i = 0; i < long_rps->nb_refs; i++) {
  419. int poc = long_rps->poc[i];
  420. int list = long_rps->used[i] ? LT_CURR : LT_FOLL;
  421. ret = add_candidate_ref(s, &rps[list], poc, HEVC_FRAME_FLAG_LONG_REF);
  422. if (ret < 0)
  423. goto fail;
  424. }
  425. fail:
  426. /* release any frames that are now unused */
  427. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++)
  428. ff_hevc_unref_frame(s, &s->DPB[i], 0);
  429. return ret;
  430. }
  431. int ff_hevc_compute_poc(HEVCContext *s, int poc_lsb)
  432. {
  433. int max_poc_lsb = 1 << s->ps.sps->log2_max_poc_lsb;
  434. int prev_poc_lsb = s->pocTid0 % max_poc_lsb;
  435. int prev_poc_msb = s->pocTid0 - prev_poc_lsb;
  436. int poc_msb;
  437. if (poc_lsb < prev_poc_lsb && prev_poc_lsb - poc_lsb >= max_poc_lsb / 2)
  438. poc_msb = prev_poc_msb + max_poc_lsb;
  439. else if (poc_lsb > prev_poc_lsb && poc_lsb - prev_poc_lsb > max_poc_lsb / 2)
  440. poc_msb = prev_poc_msb - max_poc_lsb;
  441. else
  442. poc_msb = prev_poc_msb;
  443. // For BLA picture types, POCmsb is set to 0.
  444. if (s->nal_unit_type == HEVC_NAL_BLA_W_LP ||
  445. s->nal_unit_type == HEVC_NAL_BLA_W_RADL ||
  446. s->nal_unit_type == HEVC_NAL_BLA_N_LP)
  447. poc_msb = 0;
  448. return poc_msb + poc_lsb;
  449. }
  450. int ff_hevc_frame_nb_refs(HEVCContext *s)
  451. {
  452. int ret = 0;
  453. int i;
  454. const ShortTermRPS *rps = s->sh.short_term_rps;
  455. LongTermRPS *long_rps = &s->sh.long_term_rps;
  456. if (rps) {
  457. for (i = 0; i < rps->num_negative_pics; i++)
  458. ret += !!rps->used[i];
  459. for (; i < rps->num_delta_pocs; i++)
  460. ret += !!rps->used[i];
  461. }
  462. if (long_rps) {
  463. for (i = 0; i < long_rps->nb_refs; i++)
  464. ret += !!long_rps->used[i];
  465. }
  466. return ret;
  467. }