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.

508 lines
15KB

  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 Libav.
  8. *
  9. * Libav 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. * Libav 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 Libav; 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->width + (1 << s->ps.sps->log2_ctb_size) - 1) >>
  52. s->ps.sps->log2_ctb_size;
  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. if (s->avctx->hwaccel) {
  96. const AVHWAccel *hwaccel = s->avctx->hwaccel;
  97. av_assert0(!frame->hwaccel_picture_private);
  98. if (hwaccel->frame_priv_data_size) {
  99. frame->hwaccel_priv_buf = av_buffer_allocz(hwaccel->frame_priv_data_size);
  100. if (!frame->hwaccel_priv_buf)
  101. goto fail;
  102. frame->hwaccel_picture_private = frame->hwaccel_priv_buf->data;
  103. }
  104. }
  105. return frame;
  106. fail:
  107. ff_hevc_unref_frame(s, frame, ~0);
  108. return NULL;
  109. }
  110. av_log(s->avctx, AV_LOG_ERROR, "Error allocating frame, DPB full.\n");
  111. return NULL;
  112. }
  113. int ff_hevc_set_new_ref(HEVCContext *s, AVFrame **frame, int poc)
  114. {
  115. HEVCFrame *ref;
  116. int i;
  117. /* check that this POC doesn't already exist */
  118. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  119. HEVCFrame *frame = &s->DPB[i];
  120. if (frame->frame->buf[0] && frame->sequence == s->seq_decode &&
  121. frame->poc == poc) {
  122. av_log(s->avctx, AV_LOG_ERROR, "Duplicate POC in a sequence: %d.\n",
  123. poc);
  124. return AVERROR_INVALIDDATA;
  125. }
  126. }
  127. ref = alloc_frame(s);
  128. if (!ref)
  129. return AVERROR(ENOMEM);
  130. *frame = ref->frame;
  131. s->ref = ref;
  132. if (s->sh.pic_output_flag)
  133. ref->flags = HEVC_FRAME_FLAG_OUTPUT | HEVC_FRAME_FLAG_SHORT_REF;
  134. else
  135. ref->flags = HEVC_FRAME_FLAG_SHORT_REF;
  136. ref->poc = poc;
  137. ref->sequence = s->seq_decode;
  138. ref->window = s->ps.sps->output_window;
  139. return 0;
  140. }
  141. int ff_hevc_output_frame(HEVCContext *s, AVFrame *out, int flush)
  142. {
  143. do {
  144. int nb_output = 0;
  145. int min_poc = INT_MAX;
  146. int i, min_idx, ret;
  147. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  148. HEVCFrame *frame = &s->DPB[i];
  149. if ((frame->flags & HEVC_FRAME_FLAG_OUTPUT) &&
  150. frame->sequence == s->seq_output) {
  151. nb_output++;
  152. if (frame->poc < min_poc) {
  153. min_poc = frame->poc;
  154. min_idx = i;
  155. }
  156. }
  157. }
  158. /* wait for more frames before output */
  159. if (!flush && s->seq_output == s->seq_decode && s->ps.sps &&
  160. nb_output <= s->ps.sps->temporal_layer[s->ps.sps->max_sub_layers - 1].num_reorder_pics)
  161. return 0;
  162. if (nb_output) {
  163. HEVCFrame *frame = &s->DPB[min_idx];
  164. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->frame->format);
  165. int pixel_shift;
  166. if (!desc)
  167. return AVERROR_BUG;
  168. pixel_shift = desc->comp[0].depth > 8;
  169. ret = av_frame_ref(out, frame->frame);
  170. ff_hevc_unref_frame(s, frame, HEVC_FRAME_FLAG_OUTPUT);
  171. if (ret < 0)
  172. return ret;
  173. for (i = 0; i < 3; i++) {
  174. int hshift = (i > 0) ? desc->log2_chroma_w : 0;
  175. int vshift = (i > 0) ? desc->log2_chroma_h : 0;
  176. int off = ((frame->window.left_offset >> hshift) << pixel_shift) +
  177. (frame->window.top_offset >> vshift) * out->linesize[i];
  178. out->data[i] += off;
  179. }
  180. av_log(s->avctx, AV_LOG_DEBUG,
  181. "Output frame with POC %d.\n", frame->poc);
  182. return 1;
  183. }
  184. if (s->seq_output != s->seq_decode)
  185. s->seq_output = (s->seq_output + 1) & 0xff;
  186. else
  187. break;
  188. } while (1);
  189. return 0;
  190. }
  191. static int init_slice_rpl(HEVCContext *s)
  192. {
  193. HEVCFrame *frame = s->ref;
  194. int ctb_count = frame->ctb_count;
  195. int ctb_addr_ts = s->ps.pps->ctb_addr_rs_to_ts[s->sh.slice_segment_addr];
  196. int i;
  197. if (s->slice_idx >= frame->rpl_buf->size / sizeof(RefPicListTab))
  198. return AVERROR_INVALIDDATA;
  199. for (i = ctb_addr_ts; i < ctb_count; i++)
  200. frame->rpl_tab[i] = (RefPicListTab *)frame->rpl_buf->data + s->slice_idx;
  201. frame->refPicList = (RefPicList *)frame->rpl_tab[ctb_addr_ts];
  202. return 0;
  203. }
  204. int ff_hevc_slice_rpl(HEVCContext *s)
  205. {
  206. SliceHeader *sh = &s->sh;
  207. uint8_t nb_list = sh->slice_type == B_SLICE ? 2 : 1;
  208. uint8_t list_idx;
  209. int i, j, ret;
  210. ret = init_slice_rpl(s);
  211. if (ret < 0)
  212. return ret;
  213. if (!(s->rps[ST_CURR_BEF].nb_refs + s->rps[ST_CURR_AFT].nb_refs +
  214. s->rps[LT_CURR].nb_refs)) {
  215. av_log(s->avctx, AV_LOG_ERROR, "Zero refs in the frame RPS.\n");
  216. return AVERROR_INVALIDDATA;
  217. }
  218. for (list_idx = 0; list_idx < nb_list; list_idx++) {
  219. RefPicList rpl_tmp = { { 0 } };
  220. RefPicList *rpl = &s->ref->refPicList[list_idx];
  221. /* The order of the elements is
  222. * ST_CURR_BEF - ST_CURR_AFT - LT_CURR for the L0 and
  223. * ST_CURR_AFT - ST_CURR_BEF - LT_CURR for the L1 */
  224. int cand_lists[3] = { list_idx ? ST_CURR_AFT : ST_CURR_BEF,
  225. list_idx ? ST_CURR_BEF : ST_CURR_AFT,
  226. LT_CURR };
  227. /* concatenate the candidate lists for the current frame */
  228. while (rpl_tmp.nb_refs < sh->nb_refs[list_idx]) {
  229. for (i = 0; i < FF_ARRAY_ELEMS(cand_lists); i++) {
  230. RefPicList *rps = &s->rps[cand_lists[i]];
  231. for (j = 0; j < rps->nb_refs && rpl_tmp.nb_refs < MAX_REFS; j++) {
  232. rpl_tmp.list[rpl_tmp.nb_refs] = rps->list[j];
  233. rpl_tmp.ref[rpl_tmp.nb_refs] = rps->ref[j];
  234. rpl_tmp.isLongTerm[rpl_tmp.nb_refs] = i == 2;
  235. rpl_tmp.nb_refs++;
  236. }
  237. }
  238. }
  239. /* reorder the references if necessary */
  240. if (sh->rpl_modification_flag[list_idx]) {
  241. for (i = 0; i < sh->nb_refs[list_idx]; i++) {
  242. int idx = sh->list_entry_lx[list_idx][i];
  243. if (idx >= rpl_tmp.nb_refs) {
  244. av_log(s->avctx, AV_LOG_ERROR, "Invalid reference index.\n");
  245. return AVERROR_INVALIDDATA;
  246. }
  247. rpl->list[i] = rpl_tmp.list[idx];
  248. rpl->ref[i] = rpl_tmp.ref[idx];
  249. rpl->isLongTerm[i] = rpl_tmp.isLongTerm[idx];
  250. rpl->nb_refs++;
  251. }
  252. } else {
  253. memcpy(rpl, &rpl_tmp, sizeof(*rpl));
  254. rpl->nb_refs = FFMIN(rpl->nb_refs, sh->nb_refs[list_idx]);
  255. }
  256. if (sh->collocated_list == list_idx &&
  257. sh->collocated_ref_idx < rpl->nb_refs)
  258. s->ref->collocated_ref = rpl->ref[sh->collocated_ref_idx];
  259. }
  260. return 0;
  261. }
  262. static HEVCFrame *find_ref_idx(HEVCContext *s, int poc)
  263. {
  264. int i;
  265. int LtMask = (1 << s->ps.sps->log2_max_poc_lsb) - 1;
  266. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  267. HEVCFrame *ref = &s->DPB[i];
  268. if (ref->frame->buf[0] && (ref->sequence == s->seq_decode)) {
  269. if ((ref->poc & LtMask) == poc)
  270. return ref;
  271. }
  272. }
  273. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  274. HEVCFrame *ref = &s->DPB[i];
  275. if (ref->frame->buf[0] && ref->sequence == s->seq_decode) {
  276. if (ref->poc == poc || (ref->poc & LtMask) == poc)
  277. return ref;
  278. }
  279. }
  280. av_log(s->avctx, AV_LOG_ERROR,
  281. "Could not find ref with POC %d\n", poc);
  282. return NULL;
  283. }
  284. static void mark_ref(HEVCFrame *frame, int flag)
  285. {
  286. frame->flags &= ~(HEVC_FRAME_FLAG_LONG_REF | HEVC_FRAME_FLAG_SHORT_REF);
  287. frame->flags |= flag;
  288. }
  289. static HEVCFrame *generate_missing_ref(HEVCContext *s, int poc)
  290. {
  291. HEVCFrame *frame;
  292. int i, x, y;
  293. frame = alloc_frame(s);
  294. if (!frame)
  295. return NULL;
  296. if (!s->avctx->hwaccel) {
  297. if (!s->ps.sps->pixel_shift) {
  298. for (i = 0; frame->frame->buf[i]; i++)
  299. memset(frame->frame->buf[i]->data, 1 << (s->ps.sps->bit_depth - 1),
  300. frame->frame->buf[i]->size);
  301. } else {
  302. for (i = 0; frame->frame->data[i]; i++)
  303. for (y = 0; y < (s->ps.sps->height >> s->ps.sps->vshift[i]); y++)
  304. for (x = 0; x < (s->ps.sps->width >> s->ps.sps->hshift[i]); x++) {
  305. AV_WN16(frame->frame->data[i] + y * frame->frame->linesize[i] + 2 * x,
  306. 1 << (s->ps.sps->bit_depth - 1));
  307. }
  308. }
  309. }
  310. frame->poc = poc;
  311. frame->sequence = s->seq_decode;
  312. frame->flags = 0;
  313. ff_thread_report_progress(&frame->tf, INT_MAX, 0);
  314. return frame;
  315. }
  316. /* add a reference with the given poc to the list and mark it as used in DPB */
  317. static int add_candidate_ref(HEVCContext *s, RefPicList *list,
  318. int poc, int ref_flag)
  319. {
  320. HEVCFrame *ref = find_ref_idx(s, poc);
  321. if (ref == s->ref)
  322. return AVERROR_INVALIDDATA;
  323. if (!ref) {
  324. ref = generate_missing_ref(s, poc);
  325. if (!ref)
  326. return AVERROR(ENOMEM);
  327. }
  328. list->list[list->nb_refs] = ref->poc;
  329. list->ref[list->nb_refs] = ref;
  330. list->nb_refs++;
  331. mark_ref(ref, ref_flag);
  332. return 0;
  333. }
  334. int ff_hevc_frame_rps(HEVCContext *s)
  335. {
  336. const ShortTermRPS *short_rps = s->sh.short_term_rps;
  337. const LongTermRPS *long_rps = &s->sh.long_term_rps;
  338. RefPicList *rps = s->rps;
  339. int i, ret = 0;
  340. if (!short_rps) {
  341. rps[0].nb_refs = rps[1].nb_refs = 0;
  342. return 0;
  343. }
  344. /* clear the reference flags on all frames except the current one */
  345. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++) {
  346. HEVCFrame *frame = &s->DPB[i];
  347. if (frame == s->ref)
  348. continue;
  349. mark_ref(frame, 0);
  350. }
  351. for (i = 0; i < NB_RPS_TYPE; i++)
  352. rps[i].nb_refs = 0;
  353. /* add the short refs */
  354. for (i = 0; i < short_rps->num_delta_pocs; i++) {
  355. int poc = s->poc + short_rps->delta_poc[i];
  356. int list;
  357. if (!short_rps->used[i])
  358. list = ST_FOLL;
  359. else if (i < short_rps->num_negative_pics)
  360. list = ST_CURR_BEF;
  361. else
  362. list = ST_CURR_AFT;
  363. ret = add_candidate_ref(s, &rps[list], poc, HEVC_FRAME_FLAG_SHORT_REF);
  364. if (ret < 0)
  365. goto fail;
  366. }
  367. /* add the long refs */
  368. for (i = 0; i < long_rps->nb_refs; i++) {
  369. int poc = long_rps->poc[i];
  370. int list = long_rps->used[i] ? LT_CURR : LT_FOLL;
  371. ret = add_candidate_ref(s, &rps[list], poc, HEVC_FRAME_FLAG_LONG_REF);
  372. if (ret < 0)
  373. goto fail;
  374. }
  375. fail:
  376. /* release any frames that are now unused */
  377. for (i = 0; i < FF_ARRAY_ELEMS(s->DPB); i++)
  378. ff_hevc_unref_frame(s, &s->DPB[i], 0);
  379. return ret;
  380. }
  381. int ff_hevc_compute_poc(HEVCContext *s, int poc_lsb)
  382. {
  383. int max_poc_lsb = 1 << s->ps.sps->log2_max_poc_lsb;
  384. int prev_poc_lsb = s->pocTid0 % max_poc_lsb;
  385. int prev_poc_msb = s->pocTid0 - prev_poc_lsb;
  386. int poc_msb;
  387. if (poc_lsb < prev_poc_lsb && prev_poc_lsb - poc_lsb >= max_poc_lsb / 2)
  388. poc_msb = prev_poc_msb + max_poc_lsb;
  389. else if (poc_lsb > prev_poc_lsb && poc_lsb - prev_poc_lsb > max_poc_lsb / 2)
  390. poc_msb = prev_poc_msb - max_poc_lsb;
  391. else
  392. poc_msb = prev_poc_msb;
  393. // For BLA picture types, POCmsb is set to 0.
  394. if (s->nal_unit_type == NAL_BLA_W_LP ||
  395. s->nal_unit_type == NAL_BLA_W_RADL ||
  396. s->nal_unit_type == NAL_BLA_N_LP)
  397. poc_msb = 0;
  398. return poc_msb + poc_lsb;
  399. }
  400. int ff_hevc_frame_nb_refs(HEVCContext *s)
  401. {
  402. int ret = 0;
  403. int i;
  404. const ShortTermRPS *rps = s->sh.short_term_rps;
  405. LongTermRPS *long_rps = &s->sh.long_term_rps;
  406. if (rps) {
  407. for (i = 0; i < rps->num_negative_pics; i++)
  408. ret += !!rps->used[i];
  409. for (; i < rps->num_delta_pocs; i++)
  410. ret += !!rps->used[i];
  411. }
  412. if (long_rps) {
  413. for (i = 0; i < long_rps->nb_refs; i++)
  414. ret += !!long_rps->used[i];
  415. }
  416. return ret;
  417. }