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.

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