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.

561 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. void ff_hevc_unref_frame(HEVCContext *s, HEVCFrame *frame, int flags)
  29. {
  30. /* frame->frame can be NULL if context init failed */
  31. if (!frame->frame || !frame->frame->buf[0])
  32. return;
  33. frame->flags &= ~flags;
  34. if (!frame->flags) {
  35. ff_thread_release_buffer(s->avctx, &frame->tf);
  36. av_buffer_unref(&frame->tab_mvf_buf);
  37. frame->tab_mvf = NULL;
  38. av_buffer_unref(&frame->rpl_buf);
  39. av_buffer_unref(&frame->rpl_tab_buf);
  40. frame->rpl_tab = NULL;
  41. frame->refPicList = NULL;
  42. frame->collocated_ref = NULL;
  43. av_buffer_unref(&frame->hwaccel_priv_buf);
  44. frame->hwaccel_picture_private = NULL;
  45. }
  46. }
  47. RefPicList *ff_hevc_get_ref_list(HEVCContext *s, HEVCFrame *ref, int x0, int y0)
  48. {
  49. int x_cb = x0 >> s->ps.sps->log2_ctb_size;
  50. int y_cb = y0 >> s->ps.sps->log2_ctb_size;
  51. int pic_width_cb = s->ps.sps->ctb_width;
  52. int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[y_cb * pic_width_cb + x_cb];
  53. return (RefPicList *)ref->rpl_tab[ctb_addr_ts];
  54. }
  55. void ff_hevc_clear_refs(HEVCContext *s)
  56. {
  57. int i;
  58. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++)
  59. ff_hevc_unref_frame(s, &s->DPB[i],
  60. HEVC_FRAME_FLAG_SHORT_REF |
  61. HEVC_FRAME_FLAG_LONG_REF);
  62. }
  63. void ff_hevc_flush_dpb(HEVCContext *s)
  64. {
  65. int i;
  66. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++)
  67. ff_hevc_unref_frame(s, &s->DPB[i], ~0);
  68. }
  69. static HEVCFrame *alloc_frame(HEVCContext *s)
  70. {
  71. int i, j, ret;
  72. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  73. HEVCFrame *frame = &s->DPB[i];
  74. if (frame->frame->buf[0])
  75. continue;
  76. ret = ff_thread_get_buffer(s->avctx, &frame->tf,
  77. AV_GET_BUFFER_FLAG_REF);
  78. if (ret < 0)
  79. return NULL;
  80. frame->rpl_buf = av_buffer_allocz(s->pkt.nb_nals * sizeof(RefPicListTab));
  81. if (!frame->rpl_buf)
  82. goto fail;
  83. frame->tab_mvf_buf = av_buffer_pool_get(s->tab_mvf_pool);
  84. if (!frame->tab_mvf_buf)
  85. goto fail;
  86. frame->tab_mvf = (MvField *)frame->tab_mvf_buf->data;
  87. frame->rpl_tab_buf = av_buffer_pool_get(s->rpl_tab_pool);
  88. if (!frame->rpl_tab_buf)
  89. goto fail;
  90. frame->rpl_tab = (RefPicListTab **)frame->rpl_tab_buf->data;
  91. frame->ctb_count = s->ps.sps->ctb_width * s->ps.sps->ctb_height;
  92. for (j = 0; j < frame->ctb_count; j++)
  93. frame->rpl_tab[j] = (RefPicListTab *)frame->rpl_buf->data;
  94. frame->frame->top_field_first = s->picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD;
  95. frame->frame->interlaced_frame = (s->picture_struct == AV_PICTURE_STRUCTURE_TOP_FIELD) || (s->picture_struct == AV_PICTURE_STRUCTURE_BOTTOM_FIELD);
  96. if (s->avctx->hwaccel) {
  97. const AVHWAccel *hwaccel = s->avctx->hwaccel;
  98. av_assert0(!frame->hwaccel_picture_private);
  99. if (hwaccel->frame_priv_data_size) {
  100. frame->hwaccel_priv_buf = av_buffer_allocz(hwaccel->frame_priv_data_size);
  101. if (!frame->hwaccel_priv_buf)
  102. goto fail;
  103. frame->hwaccel_picture_private = frame->hwaccel_priv_buf->data;
  104. }
  105. }
  106. return frame;
  107. fail:
  108. ff_hevc_unref_frame(s, frame, ~0);
  109. return NULL;
  110. }
  111. av_log(s->avctx, AV_LOG_ERROR, "Error allocating frame, DPB full.\n");
  112. return NULL;
  113. }
  114. int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc)
  115. {
  116. HEVCFrame *ref;
  117. int i;
  118. /* check that this POC doesn't already exist */
  119. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  120. HEVCFrame *frame = &s->DPB[i];
  121. if (frame->frame->buf[0] && frame->sequence == s->seq_decode &&
  122. frame->poc == poc) {
  123. av_log(s->avctx, AV_LOG_ERROR, "Duplicate POC in a sequence: %d.\n",
  124. poc);
  125. return AVERROR_INVALIDDATA;
  126. }
  127. }
  128. ref = alloc_frame(s);
  129. if (!ref)
  130. return AVERROR(ENOMEM);
  131. *frame = ref->frame;
  132. s->ref = ref;
  133. if (s->sh.pic_output_flag)
  134. ref->flags = HEVC_FRAME_FLAG_OUTPUT | HEVC_FRAME_FLAG_SHORT_REF;
  135. else
  136. ref->flags = HEVC_FRAME_FLAG_SHORT_REF;
  137. ref->poc = poc;
  138. ref->sequence = s->seq_decode;
  139. ref->window = s->ps.sps->output_window;
  140. return 0;
  141. }
  142. int ff_hevc_output_frame(HEVCContext *s, AVFrame *out, int flush)
  143. {
  144. do {
  145. int nb_output = 0;
  146. int min_poc = INT_MAX;
  147. int i, min_idx, ret;
  148. if (s->sh.no_output_of_prior_pics_flag == 1) {
  149. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  150. HEVCFrame *frame = &s->DPB[i];
  151. if (!(frame->flags & HEVC_FRAME_FLAG_BUMPING) && frame->poc != s->poc &&
  152. frame->sequence == s->seq_output) {
  153. ff_hevc_unref_frame(s, frame, HEVC_FRAME_FLAG_OUTPUT);
  154. }
  155. }
  156. }
  157. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  158. HEVCFrame *frame = &s->DPB[i];
  159. if ((frame->flags & HEVC_FRAME_FLAG_OUTPUT) &&
  160. frame->sequence == s->seq_output) {
  161. nb_output++;
  162. if (frame->poc < min_poc || nb_output == 1) {
  163. min_poc = frame->poc;
  164. min_idx = i;
  165. }
  166. }
  167. }
  168. /* wait for more frames before output */
  169. if (!flush && s->seq_output == s->seq_decode && s->ps.sps &&
  170. nb_output <= s->ps.sps->temporal_layer[s->ps.sps->max_sub_layers - 1].num_reorder_pics)
  171. return 0;
  172. if (nb_output) {
  173. HEVCFrame *frame = &s->DPB[min_idx];
  174. AVFrame *dst = out;
  175. AVFrame *src = frame->frame;
  176. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(src->format);
  177. int pixel_shift = !!(desc->comp[0].depth_minus1 > 7);
  178. ret = av_frame_ref(out, src);
  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. for (i = 0; i < 3; i++) {
  186. int hshift = (i > 0) ? desc->log2_chroma_w : 0;
  187. int vshift = (i > 0) ? desc->log2_chroma_h : 0;
  188. int off = ((frame->window.left_offset >> hshift) << pixel_shift) +
  189. (frame->window.top_offset >> vshift) * dst->linesize[i];
  190. dst->data[i] += off;
  191. }
  192. av_log(s->avctx, AV_LOG_DEBUG,
  193. "Output frame with POC %d.\n", frame->poc);
  194. return 1;
  195. }
  196. if (s->seq_output != s->seq_decode)
  197. s->seq_output = (s->seq_output + 1) & 0xff;
  198. else
  199. break;
  200. } while (1);
  201. return 0;
  202. }
  203. void ff_hevc_bump_frame(HEVCContext *s)
  204. {
  205. int dpb = 0;
  206. int min_poc = INT_MAX;
  207. int i;
  208. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  209. HEVCFrame *frame = &s->DPB[i];
  210. if ((frame->flags) &&
  211. frame->sequence == s->seq_output &&
  212. frame->poc != s->poc) {
  213. dpb++;
  214. }
  215. }
  216. if (s->ps.sps && dpb >= s->ps.sps->temporal_layer[s->ps.sps->max_sub_layers - 1].max_dec_pic_buffering) {
  217. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  218. HEVCFrame *frame = &s->DPB[i];
  219. if ((frame->flags) &&
  220. frame->sequence == s->seq_output &&
  221. frame->poc != s->poc) {
  222. if (frame->flags == HEVC_FRAME_FLAG_OUTPUT && frame->poc < min_poc) {
  223. min_poc = frame->poc;
  224. }
  225. }
  226. }
  227. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  228. HEVCFrame *frame = &s->DPB[i];
  229. if (frame->flags & HEVC_FRAME_FLAG_OUTPUT &&
  230. frame->sequence == s->seq_output &&
  231. frame->poc <= min_poc) {
  232. frame->flags |= HEVC_FRAME_FLAG_BUMPING;
  233. }
  234. }
  235. dpb--;
  236. }
  237. }
  238. static int init_slice_rpl(HEVCContext *s)
  239. {
  240. HEVCFrame *frame = s->ref;
  241. int ctb_count = frame->ctb_count;
  242. int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_segment_addr];
  243. int i;
  244. if (s->slice_idx >= frame->rpl_buf->size / sizeof(RefPicListTab))
  245. return AVERROR_INVALIDDATA;
  246. for (i = ctb_addr_ts; i < ctb_count; i++)
  247. frame->rpl_tab[i] = (RefPicListTab *)frame->rpl_buf->data + s->slice_idx;
  248. frame->refPicList = (RefPicList *)frame->rpl_tab[ctb_addr_ts];
  249. return 0;
  250. }
  251. int ff_hevc_slice_rpl(HEVCContext *s)
  252. {
  253. SliceHeader *sh = &s->sh;
  254. uint8_t nb_list = sh->slice_type == B_SLICE ? 2 : 1;
  255. uint8_t list_idx;
  256. int i, j, ret;
  257. ret = init_slice_rpl(s);
  258. if (ret < 0)
  259. return ret;
  260. if (!(s->rps[ST_CURR_BEF].nb_refs + s->rps[ST_CURR_AFT].nb_refs +
  261. s->rps[LT_CURR].nb_refs)) {
  262. av_log(s->avctx, AV_LOG_ERROR, "Zero refs in the frame RPS.\n");
  263. return AVERROR_INVALIDDATA;
  264. }
  265. for (list_idx = 0; list_idx < nb_list; list_idx++) {
  266. RefPicList rpl_tmp = { { 0 } };
  267. RefPicList *rpl = &s->ref->refPicList[list_idx];
  268. /* The order of the elements is
  269. * ST_CURR_BEF - ST_CURR_AFT - LT_CURR for the L0 and
  270. * ST_CURR_AFT - ST_CURR_BEF - LT_CURR for the L1 */
  271. int cand_lists[3] = { list_idx ? ST_CURR_AFT : ST_CURR_BEF,
  272. list_idx ? ST_CURR_BEF : ST_CURR_AFT,
  273. LT_CURR };
  274. /* concatenate the candidate lists for the current frame */
  275. while (rpl_tmp.nb_refs < sh->nb_refs[list_idx]) {
  276. for (i = 0; i < FF_ARRAY_ELEMS(cand_lists); i++) {
  277. RefPicList *rps = &s->rps[cand_lists[i]];
  278. for (j = 0; j < rps->nb_refs && rpl_tmp.nb_refs < MAX_REFS; j++) {
  279. rpl_tmp.list[rpl_tmp.nb_refs] = rps->list[j];
  280. rpl_tmp.ref[rpl_tmp.nb_refs] = rps->ref[j];
  281. rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = i == 2;
  282. rpl_tmp.nb_refs++;
  283. }
  284. }
  285. }
  286. /* reorder the references if necessary */
  287. if (sh->rpl_modification_flag[list_idx]) {
  288. for (i = 0; i < sh->nb_refs[list_idx]; i++) {
  289. int idx = sh->list_entry_lx[list_idx][i];
  290. if (idx >= rpl_tmp.nb_refs) {
  291. av_log(s->avctx, AV_LOG_ERROR, "Invalid reference index.\n");
  292. return AVERROR_INVALIDDATA;
  293. }
  294. rpl->list[i] = rpl_tmp.list[idx];
  295. rpl->ref[i] = rpl_tmp.ref[idx];
  296. rpl->isLongTerm[i] = rpl_tmp.isLongTerm[idx];
  297. rpl->nb_refs++;
  298. }
  299. } else {
  300. memcpy(rpl, &rpl_tmp, sizeof(*rpl));
  301. rpl->nb_refs = FFMIN(rpl->nb_refs, sh->nb_refs[list_idx]);
  302. }
  303. if (sh->collocated_list == list_idx &&
  304. sh->collocated_ref_idx < rpl->nb_refs)
  305. s->ref->collocated_ref = rpl->ref[sh->collocated_ref_idx];
  306. }
  307. return 0;
  308. }
  309. static HEVCFrame *find_ref_idx(HEVCContext *s, int poc)
  310. {
  311. int i;
  312. int LtMask = (1 << s->ps.sps->log2_max_poc_lsb) - 1;
  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 & LtMask) == poc)
  317. return ref;
  318. }
  319. }
  320. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  321. HEVCFrame *ref = &s->DPB[i];
  322. if (ref->frame->buf[0] && ref->sequence == s->seq_decode) {
  323. if (ref->poc == poc || (ref->poc & LtMask) == poc)
  324. return ref;
  325. }
  326. }
  327. if (s->nal_unit_type != NAL_CRA_NUT && !IS_BLA(s))
  328. av_log(s->avctx, AV_LOG_ERROR,
  329. "Could not find ref with POC %d\n", poc);
  330. return NULL;
  331. }
  332. static void mark_ref(HEVCFrame *frame, int flag)
  333. {
  334. frame->flags &= ~(HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF);
  335. frame->flags |= flag;
  336. }
  337. static HEVCFrame *generate_missing_ref(HEVCContext *s, int poc)
  338. {
  339. HEVCFrame *frame;
  340. int i, x, y;
  341. frame = alloc_frame(s);
  342. if (!frame)
  343. return NULL;
  344. if (!s->avctx->hwaccel) {
  345. if (!s->ps.sps->pixel_shift) {
  346. for (i = 0; frame->frame->buf[i]; i++)
  347. memset(frame->frame->buf[i]->data, 1 << (s->ps.sps->bit_depth - 1),
  348. frame->frame->buf[i]->size);
  349. } else {
  350. for (i = 0; frame->frame->data[i]; i++)
  351. for (y = 0; y < (s->ps.sps->height >> s->ps.sps->vshift[i]); y++)
  352. for (x = 0; x < (s->ps.sps->width >> s->ps.sps->hshift[i]); x++) {
  353. AV_WN16(frame->frame->data[i] + y * frame->frame->linesize[i] + 2 * x,
  354. 1 << (s->ps.sps->bit_depth - 1));
  355. }
  356. }
  357. }
  358. frame->poc = poc;
  359. frame->sequence = s->seq_decode;
  360. frame->flags = 0;
  361. if (s->threads_type == FF_THREAD_FRAME)
  362. ff_thread_report_progress(&frame->tf, INT_MAX, 0);
  363. return frame;
  364. }
  365. /* add a reference with the given poc to the list and mark it as used in DPB */
  366. static int add_candidate_ref(HEVCContext *s, RefPicList *list,
  367. int poc, int ref_flag)
  368. {
  369. HEVCFrame *ref = find_ref_idx(s, poc);
  370. if (ref == s->ref)
  371. return AVERROR_INVALIDDATA;
  372. if (!ref) {
  373. ref = generate_missing_ref(s, poc);
  374. if (!ref)
  375. return AVERROR(ENOMEM);
  376. }
  377. list->list[list->nb_refs] = ref->poc;
  378. list->ref[list->nb_refs] = ref;
  379. list->nb_refs++;
  380. mark_ref(ref, ref_flag);
  381. return 0;
  382. }
  383. int ff_hevc_frame_rps(HEVCContext *s)
  384. {
  385. const ShortTermRPS *short_rps = s->sh.short_term_rps;
  386. const LongTermRPS *long_rps = &s->sh.long_term_rps;
  387. RefPicList *rps = s->rps;
  388. int i, ret = 0;
  389. if (!short_rps) {
  390. rps[0].nb_refs = rps[1].nb_refs = 0;
  391. return 0;
  392. }
  393. /* clear the reference flags on all frames except the current one */
  394. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  395. HEVCFrame *frame = &s->DPB[i];
  396. if (frame == s->ref)
  397. continue;
  398. mark_ref(frame, 0);
  399. }
  400. for (i = 0; i < NB_RPS_TYPE; i++)
  401. rps[i].nb_refs = 0;
  402. /* add the short refs */
  403. for (i = 0; i < short_rps->num_delta_pocs; i++) {
  404. int poc = s->poc + short_rps->delta_poc[i];
  405. int list;
  406. if (!short_rps->used[i])
  407. list = ST_FOLL;
  408. else if (i < short_rps->num_negative_pics)
  409. list = ST_CURR_BEF;
  410. else
  411. list = ST_CURR_AFT;
  412. ret = add_candidate_ref(s, &rps[list], poc, HEVC_FRAME_FLAG_SHORT_REF);
  413. if (ret < 0)
  414. goto fail;
  415. }
  416. /* add the long refs */
  417. for (i = 0; i < long_rps->nb_refs; i++) {
  418. int poc = long_rps->poc[i];
  419. int list = long_rps->used[i] ? LT_CURR : LT_FOLL;
  420. ret = add_candidate_ref(s, &rps[list], poc, HEVC_FRAME_FLAG_LONG_REF);
  421. if (ret < 0)
  422. goto fail;
  423. }
  424. fail:
  425. /* release any frames that are now unused */
  426. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++)
  427. ff_hevc_unref_frame(s, &s->DPB[i], 0);
  428. return ret;
  429. }
  430. int ff_hevc_compute_poc(HEVCContext *s, int poc_lsb)
  431. {
  432. int max_poc_lsb = 1 << s->ps.sps->log2_max_poc_lsb;
  433. int prev_poc_lsb = s->pocTid0 % max_poc_lsb;
  434. int prev_poc_msb = s->pocTid0 - prev_poc_lsb;
  435. int poc_msb;
  436. if (poc_lsb < prev_poc_lsb && prev_poc_lsb - poc_lsb >= max_poc_lsb / 2)
  437. poc_msb = prev_poc_msb + max_poc_lsb;
  438. else if (poc_lsb > prev_poc_lsb && poc_lsb - prev_poc_lsb > max_poc_lsb / 2)
  439. poc_msb = prev_poc_msb - max_poc_lsb;
  440. else
  441. poc_msb = prev_poc_msb;
  442. // For BLA picture types, POCmsb is set to 0.
  443. if (s->nal_unit_type == NAL_BLA_W_LP ||
  444. s->nal_unit_type == NAL_BLA_W_RADL ||
  445. s->nal_unit_type == NAL_BLA_N_LP)
  446. poc_msb = 0;
  447. return poc_msb + poc_lsb;
  448. }
  449. int ff_hevc_frame_nb_refs(HEVCContext *s)
  450. {
  451. int ret = 0;
  452. int i;
  453. const ShortTermRPS *rps = s->sh.short_term_rps;
  454. LongTermRPS *long_rps = &s->sh.long_term_rps;
  455. if (rps) {
  456. for (i = 0; i < rps->num_negative_pics; i++)
  457. ret += !!rps->used[i];
  458. for (; i < rps->num_delta_pocs; i++)
  459. ret += !!rps->used[i];
  460. }
  461. if (long_rps) {
  462. for (i = 0; i < long_rps->nb_refs; i++)
  463. ret += !!long_rps->used[i];
  464. }
  465. return ret;
  466. }