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.

1270 lines
42KB

  1. /*
  2. * H.26L/H.264/AVC/JVT/14496-10/... decoder
  3. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * H.264 / AVC / MPEG-4 part10 codec.
  24. * @author Michael Niedermayer <michaelni@gmx.at>
  25. */
  26. #define UNCHECKED_BITSTREAM_READER 1
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/display.h"
  29. #include "libavutil/imgutils.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/stereo3d.h"
  32. #include "libavutil/timer.h"
  33. #include "internal.h"
  34. #include "bytestream.h"
  35. #include "cabac.h"
  36. #include "cabac_functions.h"
  37. #include "error_resilience.h"
  38. #include "avcodec.h"
  39. #include "h264.h"
  40. #include "h264dec.h"
  41. #include "h2645_parse.h"
  42. #include "h264data.h"
  43. #include "h264chroma.h"
  44. #include "h264_mvpred.h"
  45. #include "h264_ps.h"
  46. #include "golomb.h"
  47. #include "mathops.h"
  48. #include "me_cmp.h"
  49. #include "mpegutils.h"
  50. #include "profiles.h"
  51. #include "rectangle.h"
  52. #include "thread.h"
  53. #include "vdpau_compat.h"
  54. static int h264_decode_end(AVCodecContext *avctx);
  55. const uint16_t ff_h264_mb_sizes[4] = { 256, 384, 512, 768 };
  56. int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx)
  57. {
  58. H264Context *h = avctx->priv_data;
  59. return h && h->ps.sps ? h->ps.sps->num_reorder_frames : 0;
  60. }
  61. static void h264_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
  62. int (*mv)[2][4][2],
  63. int mb_x, int mb_y, int mb_intra, int mb_skipped)
  64. {
  65. H264Context *h = opaque;
  66. H264SliceContext *sl = &h->slice_ctx[0];
  67. sl->mb_x = mb_x;
  68. sl->mb_y = mb_y;
  69. sl->mb_xy = mb_x + mb_y * h->mb_stride;
  70. memset(sl->non_zero_count_cache, 0, sizeof(sl->non_zero_count_cache));
  71. av_assert1(ref >= 0);
  72. /* FIXME: It is possible albeit uncommon that slice references
  73. * differ between slices. We take the easy approach and ignore
  74. * it for now. If this turns out to have any relevance in
  75. * practice then correct remapping should be added. */
  76. if (ref >= sl->ref_count[0])
  77. ref = 0;
  78. if (!sl->ref_list[0][ref].data[0]) {
  79. av_log(h->avctx, AV_LOG_DEBUG, "Reference not available for error concealing\n");
  80. ref = 0;
  81. }
  82. if ((sl->ref_list[0][ref].reference&3) != 3) {
  83. av_log(h->avctx, AV_LOG_DEBUG, "Reference invalid\n");
  84. return;
  85. }
  86. fill_rectangle(&h->cur_pic.ref_index[0][4 * sl->mb_xy],
  87. 2, 2, 2, ref, 1);
  88. fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  89. fill_rectangle(sl->mv_cache[0][scan8[0]], 4, 4, 8,
  90. pack16to32((*mv)[0][0][0], (*mv)[0][0][1]), 4);
  91. sl->mb_mbaff =
  92. sl->mb_field_decoding_flag = 0;
  93. ff_h264_hl_decode_mb(h, &h->slice_ctx[0]);
  94. }
  95. void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl,
  96. int y, int height)
  97. {
  98. AVCodecContext *avctx = h->avctx;
  99. const AVFrame *src = h->cur_pic.f;
  100. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  101. int vshift = desc->log2_chroma_h;
  102. const int field_pic = h->picture_structure != PICT_FRAME;
  103. if (field_pic) {
  104. height <<= 1;
  105. y <<= 1;
  106. }
  107. height = FFMIN(height, avctx->height - y);
  108. if (field_pic && h->first_field && !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
  109. return;
  110. if (avctx->draw_horiz_band) {
  111. int offset[AV_NUM_DATA_POINTERS];
  112. int i;
  113. offset[0] = y * src->linesize[0];
  114. offset[1] =
  115. offset[2] = (y >> vshift) * src->linesize[1];
  116. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  117. offset[i] = 0;
  118. emms_c();
  119. avctx->draw_horiz_band(avctx, src, offset,
  120. y, h->picture_structure, height);
  121. }
  122. }
  123. void ff_h264_free_tables(H264Context *h)
  124. {
  125. int i;
  126. av_freep(&h->intra4x4_pred_mode);
  127. av_freep(&h->chroma_pred_mode_table);
  128. av_freep(&h->cbp_table);
  129. av_freep(&h->mvd_table[0]);
  130. av_freep(&h->mvd_table[1]);
  131. av_freep(&h->direct_table);
  132. av_freep(&h->non_zero_count);
  133. av_freep(&h->slice_table_base);
  134. h->slice_table = NULL;
  135. av_freep(&h->list_counts);
  136. av_freep(&h->mb2b_xy);
  137. av_freep(&h->mb2br_xy);
  138. av_buffer_pool_uninit(&h->qscale_table_pool);
  139. av_buffer_pool_uninit(&h->mb_type_pool);
  140. av_buffer_pool_uninit(&h->motion_val_pool);
  141. av_buffer_pool_uninit(&h->ref_index_pool);
  142. for (i = 0; i < h->nb_slice_ctx; i++) {
  143. H264SliceContext *sl = &h->slice_ctx[i];
  144. av_freep(&sl->dc_val_base);
  145. av_freep(&sl->er.mb_index2xy);
  146. av_freep(&sl->er.error_status_table);
  147. av_freep(&sl->er.er_temp_buffer);
  148. av_freep(&sl->bipred_scratchpad);
  149. av_freep(&sl->edge_emu_buffer);
  150. av_freep(&sl->top_borders[0]);
  151. av_freep(&sl->top_borders[1]);
  152. sl->bipred_scratchpad_allocated = 0;
  153. sl->edge_emu_buffer_allocated = 0;
  154. sl->top_borders_allocated[0] = 0;
  155. sl->top_borders_allocated[1] = 0;
  156. }
  157. }
  158. int ff_h264_alloc_tables(H264Context *h)
  159. {
  160. const int big_mb_num = h->mb_stride * (h->mb_height + 1);
  161. const int row_mb_num = 2*h->mb_stride*FFMAX(h->nb_slice_ctx, 1);
  162. int x, y;
  163. FF_ALLOCZ_ARRAY_OR_GOTO(h->avctx, h->intra4x4_pred_mode,
  164. row_mb_num, 8 * sizeof(uint8_t), fail)
  165. h->slice_ctx[0].intra4x4_pred_mode = h->intra4x4_pred_mode;
  166. FF_ALLOCZ_OR_GOTO(h->avctx, h->non_zero_count,
  167. big_mb_num * 48 * sizeof(uint8_t), fail)
  168. FF_ALLOCZ_OR_GOTO(h->avctx, h->slice_table_base,
  169. (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base), fail)
  170. FF_ALLOCZ_OR_GOTO(h->avctx, h->cbp_table,
  171. big_mb_num * sizeof(uint16_t), fail)
  172. FF_ALLOCZ_OR_GOTO(h->avctx, h->chroma_pred_mode_table,
  173. big_mb_num * sizeof(uint8_t), fail)
  174. FF_ALLOCZ_ARRAY_OR_GOTO(h->avctx, h->mvd_table[0],
  175. row_mb_num, 16 * sizeof(uint8_t), fail);
  176. FF_ALLOCZ_ARRAY_OR_GOTO(h->avctx, h->mvd_table[1],
  177. row_mb_num, 16 * sizeof(uint8_t), fail);
  178. h->slice_ctx[0].mvd_table[0] = h->mvd_table[0];
  179. h->slice_ctx[0].mvd_table[1] = h->mvd_table[1];
  180. FF_ALLOCZ_OR_GOTO(h->avctx, h->direct_table,
  181. 4 * big_mb_num * sizeof(uint8_t), fail);
  182. FF_ALLOCZ_OR_GOTO(h->avctx, h->list_counts,
  183. big_mb_num * sizeof(uint8_t), fail)
  184. memset(h->slice_table_base, -1,
  185. (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base));
  186. h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1;
  187. FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2b_xy,
  188. big_mb_num * sizeof(uint32_t), fail);
  189. FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2br_xy,
  190. big_mb_num * sizeof(uint32_t), fail);
  191. for (y = 0; y < h->mb_height; y++)
  192. for (x = 0; x < h->mb_width; x++) {
  193. const int mb_xy = x + y * h->mb_stride;
  194. const int b_xy = 4 * x + 4 * y * h->b_stride;
  195. h->mb2b_xy[mb_xy] = b_xy;
  196. h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride)));
  197. }
  198. return 0;
  199. fail:
  200. ff_h264_free_tables(h);
  201. return AVERROR(ENOMEM);
  202. }
  203. /**
  204. * Init context
  205. * Allocate buffers which are not shared amongst multiple threads.
  206. */
  207. int ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl)
  208. {
  209. ERContext *er = &sl->er;
  210. int mb_array_size = h->mb_height * h->mb_stride;
  211. int y_size = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
  212. int c_size = h->mb_stride * (h->mb_height + 1);
  213. int yc_size = y_size + 2 * c_size;
  214. int x, y, i;
  215. sl->ref_cache[0][scan8[5] + 1] =
  216. sl->ref_cache[0][scan8[7] + 1] =
  217. sl->ref_cache[0][scan8[13] + 1] =
  218. sl->ref_cache[1][scan8[5] + 1] =
  219. sl->ref_cache[1][scan8[7] + 1] =
  220. sl->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
  221. if (sl != h->slice_ctx) {
  222. memset(er, 0, sizeof(*er));
  223. } else
  224. if (CONFIG_ERROR_RESILIENCE) {
  225. /* init ER */
  226. er->avctx = h->avctx;
  227. er->decode_mb = h264_er_decode_mb;
  228. er->opaque = h;
  229. er->quarter_sample = 1;
  230. er->mb_num = h->mb_num;
  231. er->mb_width = h->mb_width;
  232. er->mb_height = h->mb_height;
  233. er->mb_stride = h->mb_stride;
  234. er->b8_stride = h->mb_width * 2 + 1;
  235. // error resilience code looks cleaner with this
  236. FF_ALLOCZ_OR_GOTO(h->avctx, er->mb_index2xy,
  237. (h->mb_num + 1) * sizeof(int), fail);
  238. for (y = 0; y < h->mb_height; y++)
  239. for (x = 0; x < h->mb_width; x++)
  240. er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
  241. er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
  242. h->mb_stride + h->mb_width;
  243. FF_ALLOCZ_OR_GOTO(h->avctx, er->error_status_table,
  244. mb_array_size * sizeof(uint8_t), fail);
  245. FF_ALLOC_OR_GOTO(h->avctx, er->er_temp_buffer,
  246. h->mb_height * h->mb_stride, fail);
  247. FF_ALLOCZ_OR_GOTO(h->avctx, sl->dc_val_base,
  248. yc_size * sizeof(int16_t), fail);
  249. er->dc_val[0] = sl->dc_val_base + h->mb_width * 2 + 2;
  250. er->dc_val[1] = sl->dc_val_base + y_size + h->mb_stride + 1;
  251. er->dc_val[2] = er->dc_val[1] + c_size;
  252. for (i = 0; i < yc_size; i++)
  253. sl->dc_val_base[i] = 1024;
  254. }
  255. return 0;
  256. fail:
  257. return AVERROR(ENOMEM); // ff_h264_free_tables will clean up for us
  258. }
  259. static int h264_init_context(AVCodecContext *avctx, H264Context *h)
  260. {
  261. int i;
  262. h->avctx = avctx;
  263. h->backup_width = -1;
  264. h->backup_height = -1;
  265. h->backup_pix_fmt = AV_PIX_FMT_NONE;
  266. h->cur_chroma_format_idc = -1;
  267. h->picture_structure = PICT_FRAME;
  268. h->workaround_bugs = avctx->workaround_bugs;
  269. h->flags = avctx->flags;
  270. h->poc.prev_poc_msb = 1 << 16;
  271. h->recovery_frame = -1;
  272. h->frame_recovered = 0;
  273. h->poc.prev_frame_num = -1;
  274. h->sei.frame_packing.frame_packing_arrangement_cancel_flag = -1;
  275. h->sei.unregistered.x264_build = -1;
  276. h->next_outputed_poc = INT_MIN;
  277. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  278. h->last_pocs[i] = INT_MIN;
  279. ff_h264_sei_uninit(&h->sei);
  280. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  281. h->nb_slice_ctx = (avctx->active_thread_type & FF_THREAD_SLICE) ? avctx->thread_count : 1;
  282. h->slice_ctx = av_mallocz_array(h->nb_slice_ctx, sizeof(*h->slice_ctx));
  283. if (!h->slice_ctx) {
  284. h->nb_slice_ctx = 0;
  285. return AVERROR(ENOMEM);
  286. }
  287. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
  288. h->DPB[i].f = av_frame_alloc();
  289. if (!h->DPB[i].f)
  290. return AVERROR(ENOMEM);
  291. }
  292. h->cur_pic.f = av_frame_alloc();
  293. if (!h->cur_pic.f)
  294. return AVERROR(ENOMEM);
  295. h->last_pic_for_ec.f = av_frame_alloc();
  296. if (!h->last_pic_for_ec.f)
  297. return AVERROR(ENOMEM);
  298. for (i = 0; i < h->nb_slice_ctx; i++)
  299. h->slice_ctx[i].h264 = h;
  300. return 0;
  301. }
  302. static av_cold int h264_decode_end(AVCodecContext *avctx)
  303. {
  304. H264Context *h = avctx->priv_data;
  305. int i;
  306. ff_h264_remove_all_refs(h);
  307. ff_h264_free_tables(h);
  308. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
  309. ff_h264_unref_picture(h, &h->DPB[i]);
  310. av_frame_free(&h->DPB[i].f);
  311. }
  312. memset(h->delayed_pic, 0, sizeof(h->delayed_pic));
  313. h->cur_pic_ptr = NULL;
  314. av_freep(&h->slice_ctx);
  315. h->nb_slice_ctx = 0;
  316. ff_h264_sei_uninit(&h->sei);
  317. ff_h264_ps_uninit(&h->ps);
  318. ff_h2645_packet_uninit(&h->pkt);
  319. ff_h264_unref_picture(h, &h->cur_pic);
  320. av_frame_free(&h->cur_pic.f);
  321. ff_h264_unref_picture(h, &h->last_pic_for_ec);
  322. av_frame_free(&h->last_pic_for_ec.f);
  323. return 0;
  324. }
  325. static AVOnce h264_vlc_init = AV_ONCE_INIT;
  326. av_cold int ff_h264_decode_init(AVCodecContext *avctx)
  327. {
  328. H264Context *h = avctx->priv_data;
  329. int ret;
  330. ret = h264_init_context(avctx, h);
  331. if (ret < 0)
  332. return ret;
  333. ret = ff_thread_once(&h264_vlc_init, ff_h264_decode_init_vlc);
  334. if (ret != 0) {
  335. av_log(avctx, AV_LOG_ERROR, "pthread_once has failed.");
  336. return AVERROR_UNKNOWN;
  337. }
  338. if (avctx->ticks_per_frame == 1) {
  339. if(h->avctx->time_base.den < INT_MAX/2) {
  340. h->avctx->time_base.den *= 2;
  341. } else
  342. h->avctx->time_base.num /= 2;
  343. }
  344. avctx->ticks_per_frame = 2;
  345. if (avctx->extradata_size > 0 && avctx->extradata) {
  346. ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
  347. &h->ps, &h->is_avc, &h->nal_length_size,
  348. avctx->err_recognition, avctx);
  349. if (ret < 0) {
  350. h264_decode_end(avctx);
  351. return ret;
  352. }
  353. }
  354. if (h->ps.sps && h->ps.sps->bitstream_restriction_flag &&
  355. h->avctx->has_b_frames < h->ps.sps->num_reorder_frames) {
  356. h->avctx->has_b_frames = h->ps.sps->num_reorder_frames;
  357. }
  358. avctx->internal->allocate_progress = 1;
  359. ff_h264_flush_change(h);
  360. if (h->enable_er < 0 && (avctx->active_thread_type & FF_THREAD_SLICE))
  361. h->enable_er = 0;
  362. if (h->enable_er && (avctx->active_thread_type & FF_THREAD_SLICE)) {
  363. av_log(avctx, AV_LOG_WARNING,
  364. "Error resilience with slice threads is enabled. It is unsafe and unsupported and may crash. "
  365. "Use it at your own risk\n");
  366. }
  367. return 0;
  368. }
  369. #if HAVE_THREADS
  370. static int decode_init_thread_copy(AVCodecContext *avctx)
  371. {
  372. H264Context *h = avctx->priv_data;
  373. int ret;
  374. if (!avctx->internal->is_copy)
  375. return 0;
  376. memset(h, 0, sizeof(*h));
  377. ret = h264_init_context(avctx, h);
  378. if (ret < 0)
  379. return ret;
  380. h->context_initialized = 0;
  381. return 0;
  382. }
  383. #endif
  384. /**
  385. * Run setup operations that must be run after slice header decoding.
  386. * This includes finding the next displayed frame.
  387. *
  388. * @param h h264 master context
  389. * @param setup_finished enough NALs have been read that we can call
  390. * ff_thread_finish_setup()
  391. */
  392. static void decode_postinit(H264Context *h, int setup_finished)
  393. {
  394. const SPS *sps = h->ps.sps;
  395. H264Picture *out = h->cur_pic_ptr;
  396. H264Picture *cur = h->cur_pic_ptr;
  397. int i, pics, out_of_order, out_idx;
  398. if (h->next_output_pic)
  399. return;
  400. if (cur->field_poc[0] == INT_MAX || cur->field_poc[1] == INT_MAX) {
  401. /* FIXME: if we have two PAFF fields in one packet, we can't start
  402. * the next thread here. If we have one field per packet, we can.
  403. * The check in decode_nal_units() is not good enough to find this
  404. * yet, so we assume the worst for now. */
  405. // if (setup_finished)
  406. // ff_thread_finish_setup(h->avctx);
  407. if (cur->field_poc[0] == INT_MAX && cur->field_poc[1] == INT_MAX)
  408. return;
  409. if (h->avctx->hwaccel || h->missing_fields <=1)
  410. return;
  411. }
  412. cur->mmco_reset = h->mmco_reset;
  413. h->mmco_reset = 0;
  414. // FIXME do something with unavailable reference frames
  415. /* Sort B-frames into display order */
  416. if (sps->bitstream_restriction_flag ||
  417. h->avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT) {
  418. h->avctx->has_b_frames = FFMAX(h->avctx->has_b_frames, sps->num_reorder_frames);
  419. }
  420. for (i = 0; 1; i++) {
  421. if(i == MAX_DELAYED_PIC_COUNT || cur->poc < h->last_pocs[i]){
  422. if(i)
  423. h->last_pocs[i-1] = cur->poc;
  424. break;
  425. } else if(i) {
  426. h->last_pocs[i-1]= h->last_pocs[i];
  427. }
  428. }
  429. out_of_order = MAX_DELAYED_PIC_COUNT - i;
  430. if( cur->f->pict_type == AV_PICTURE_TYPE_B
  431. || (h->last_pocs[MAX_DELAYED_PIC_COUNT-2] > INT_MIN && h->last_pocs[MAX_DELAYED_PIC_COUNT-1] - h->last_pocs[MAX_DELAYED_PIC_COUNT-2] > 2))
  432. out_of_order = FFMAX(out_of_order, 1);
  433. if (out_of_order == MAX_DELAYED_PIC_COUNT) {
  434. av_log(h->avctx, AV_LOG_VERBOSE, "Invalid POC %d<%d\n", cur->poc, h->last_pocs[0]);
  435. for (i = 1; i < MAX_DELAYED_PIC_COUNT; i++)
  436. h->last_pocs[i] = INT_MIN;
  437. h->last_pocs[0] = cur->poc;
  438. cur->mmco_reset = 1;
  439. } else if(h->avctx->has_b_frames < out_of_order && !sps->bitstream_restriction_flag){
  440. int loglevel = h->avctx->frame_number > 1 ? AV_LOG_WARNING : AV_LOG_VERBOSE;
  441. av_log(h->avctx, loglevel, "Increasing reorder buffer to %d\n", out_of_order);
  442. h->avctx->has_b_frames = out_of_order;
  443. }
  444. pics = 0;
  445. while (h->delayed_pic[pics])
  446. pics++;
  447. av_assert0(pics <= MAX_DELAYED_PIC_COUNT);
  448. h->delayed_pic[pics++] = cur;
  449. if (cur->reference == 0)
  450. cur->reference = DELAYED_PIC_REF;
  451. out = h->delayed_pic[0];
  452. out_idx = 0;
  453. for (i = 1; h->delayed_pic[i] &&
  454. !h->delayed_pic[i]->f->key_frame &&
  455. !h->delayed_pic[i]->mmco_reset;
  456. i++)
  457. if (h->delayed_pic[i]->poc < out->poc) {
  458. out = h->delayed_pic[i];
  459. out_idx = i;
  460. }
  461. if (h->avctx->has_b_frames == 0 &&
  462. (h->delayed_pic[0]->f->key_frame || h->delayed_pic[0]->mmco_reset))
  463. h->next_outputed_poc = INT_MIN;
  464. out_of_order = out->poc < h->next_outputed_poc;
  465. if (out_of_order || pics > h->avctx->has_b_frames) {
  466. out->reference &= ~DELAYED_PIC_REF;
  467. for (i = out_idx; h->delayed_pic[i]; i++)
  468. h->delayed_pic[i] = h->delayed_pic[i + 1];
  469. }
  470. if (!out_of_order && pics > h->avctx->has_b_frames) {
  471. h->next_output_pic = out;
  472. if (out_idx == 0 && h->delayed_pic[0] && (h->delayed_pic[0]->f->key_frame || h->delayed_pic[0]->mmco_reset)) {
  473. h->next_outputed_poc = INT_MIN;
  474. } else
  475. h->next_outputed_poc = out->poc;
  476. } else {
  477. av_log(h->avctx, AV_LOG_DEBUG, "no picture %s\n", out_of_order ? "ooo" : "");
  478. }
  479. if (h->next_output_pic) {
  480. if (h->next_output_pic->recovered) {
  481. // We have reached an recovery point and all frames after it in
  482. // display order are "recovered".
  483. h->frame_recovered |= FRAME_RECOVERED_SEI;
  484. }
  485. h->next_output_pic->recovered |= !!(h->frame_recovered & FRAME_RECOVERED_SEI);
  486. }
  487. if (setup_finished && !h->avctx->hwaccel) {
  488. ff_thread_finish_setup(h->avctx);
  489. if (h->avctx->active_thread_type & FF_THREAD_FRAME)
  490. h->setup_finished = 1;
  491. }
  492. }
  493. /**
  494. * instantaneous decoder refresh.
  495. */
  496. static void idr(H264Context *h)
  497. {
  498. int i;
  499. ff_h264_remove_all_refs(h);
  500. h->poc.prev_frame_num =
  501. h->poc.prev_frame_num_offset = 0;
  502. h->poc.prev_poc_msb = 1<<16;
  503. h->poc.prev_poc_lsb = 0;
  504. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  505. h->last_pocs[i] = INT_MIN;
  506. }
  507. /* forget old pics after a seek */
  508. void ff_h264_flush_change(H264Context *h)
  509. {
  510. int i, j;
  511. h->next_outputed_poc = INT_MIN;
  512. h->prev_interlaced_frame = 1;
  513. idr(h);
  514. h->poc.prev_frame_num = -1;
  515. if (h->cur_pic_ptr) {
  516. h->cur_pic_ptr->reference = 0;
  517. for (j=i=0; h->delayed_pic[i]; i++)
  518. if (h->delayed_pic[i] != h->cur_pic_ptr)
  519. h->delayed_pic[j++] = h->delayed_pic[i];
  520. h->delayed_pic[j] = NULL;
  521. }
  522. ff_h264_unref_picture(h, &h->last_pic_for_ec);
  523. h->first_field = 0;
  524. ff_h264_sei_uninit(&h->sei);
  525. h->recovery_frame = -1;
  526. h->frame_recovered = 0;
  527. h->current_slice = 0;
  528. h->mmco_reset = 1;
  529. }
  530. /* forget old pics after a seek */
  531. static void flush_dpb(AVCodecContext *avctx)
  532. {
  533. H264Context *h = avctx->priv_data;
  534. int i;
  535. memset(h->delayed_pic, 0, sizeof(h->delayed_pic));
  536. ff_h264_flush_change(h);
  537. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
  538. ff_h264_unref_picture(h, &h->DPB[i]);
  539. h->cur_pic_ptr = NULL;
  540. ff_h264_unref_picture(h, &h->cur_pic);
  541. h->mb_y = 0;
  542. ff_h264_free_tables(h);
  543. h->context_initialized = 0;
  544. }
  545. #if FF_API_CAP_VDPAU
  546. static const uint8_t start_code[] = { 0x00, 0x00, 0x01 };
  547. #endif
  548. static int get_last_needed_nal(H264Context *h)
  549. {
  550. int nals_needed = 0;
  551. int first_slice = 0;
  552. int i;
  553. int ret;
  554. for (i = 0; i < h->pkt.nb_nals; i++) {
  555. H2645NAL *nal = &h->pkt.nals[i];
  556. GetBitContext gb;
  557. /* packets can sometimes contain multiple PPS/SPS,
  558. * e.g. two PAFF field pictures in one packet, or a demuxer
  559. * which splits NALs strangely if so, when frame threading we
  560. * can't start the next thread until we've read all of them */
  561. switch (nal->type) {
  562. case H264_NAL_SPS:
  563. case H264_NAL_PPS:
  564. nals_needed = i;
  565. break;
  566. case H264_NAL_DPA:
  567. case H264_NAL_IDR_SLICE:
  568. case H264_NAL_SLICE:
  569. ret = init_get_bits8(&gb, nal->data + 1, (nal->size - 1));
  570. if (ret < 0)
  571. return ret;
  572. if (!get_ue_golomb_long(&gb) || // first_mb_in_slice
  573. !first_slice ||
  574. first_slice != nal->type)
  575. nals_needed = i;
  576. if (!first_slice)
  577. first_slice = nal->type;
  578. }
  579. }
  580. return nals_needed;
  581. }
  582. static void debug_green_metadata(const H264SEIGreenMetaData *gm, void *logctx)
  583. {
  584. av_log(logctx, AV_LOG_DEBUG, "Green Metadata Info SEI message\n");
  585. av_log(logctx, AV_LOG_DEBUG, " green_metadata_type: %d\n", gm->green_metadata_type);
  586. if (gm->green_metadata_type == 0) {
  587. av_log(logctx, AV_LOG_DEBUG, " green_metadata_period_type: %d\n", gm->period_type);
  588. if (gm->period_type == 2)
  589. av_log(logctx, AV_LOG_DEBUG, " green_metadata_num_seconds: %d\n", gm->num_seconds);
  590. else if (gm->period_type == 3)
  591. av_log(logctx, AV_LOG_DEBUG, " green_metadata_num_pictures: %d\n", gm->num_pictures);
  592. av_log(logctx, AV_LOG_DEBUG, " SEI GREEN Complexity Metrics: %f %f %f %f\n",
  593. (float)gm->percent_non_zero_macroblocks/255,
  594. (float)gm->percent_intra_coded_macroblocks/255,
  595. (float)gm->percent_six_tap_filtering/255,
  596. (float)gm->percent_alpha_point_deblocking_instance/255);
  597. } else if (gm->green_metadata_type == 1) {
  598. av_log(logctx, AV_LOG_DEBUG, " xsd_metric_type: %d\n", gm->xsd_metric_type);
  599. if (gm->xsd_metric_type == 0)
  600. av_log(logctx, AV_LOG_DEBUG, " xsd_metric_value: %f\n",
  601. (float)gm->xsd_metric_value/100);
  602. }
  603. }
  604. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size)
  605. {
  606. AVCodecContext *const avctx = h->avctx;
  607. unsigned context_count = 0;
  608. int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
  609. int idr_cleared=0;
  610. int i, ret = 0;
  611. h->nal_unit_type= 0;
  612. h->max_contexts = h->nb_slice_ctx;
  613. if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)) {
  614. h->current_slice = 0;
  615. if (!h->first_field)
  616. h->cur_pic_ptr = NULL;
  617. ff_h264_sei_uninit(&h->sei);
  618. }
  619. if (h->nal_length_size == 4) {
  620. if (buf_size > 8 && AV_RB32(buf) == 1 && AV_RB32(buf+5) > (unsigned)buf_size) {
  621. h->is_avc = 0;
  622. }else if(buf_size > 3 && AV_RB32(buf) > 1 && AV_RB32(buf) <= (unsigned)buf_size)
  623. h->is_avc = 1;
  624. }
  625. ret = ff_h2645_packet_split(&h->pkt, buf, buf_size, avctx, h->is_avc,
  626. h->nal_length_size, avctx->codec_id, avctx->flags2 & AV_CODEC_FLAG2_FAST);
  627. if (ret < 0) {
  628. av_log(avctx, AV_LOG_ERROR,
  629. "Error splitting the input into NAL units.\n");
  630. return ret;
  631. }
  632. if (avctx->active_thread_type & FF_THREAD_FRAME)
  633. nals_needed = get_last_needed_nal(h);
  634. if (nals_needed < 0)
  635. return nals_needed;
  636. for (i = 0; i < h->pkt.nb_nals; i++) {
  637. H2645NAL *nal = &h->pkt.nals[i];
  638. H264SliceContext *sl = &h->slice_ctx[context_count];
  639. int err;
  640. if (avctx->skip_frame >= AVDISCARD_NONREF &&
  641. nal->ref_idc == 0 && nal->type != H264_NAL_SEI)
  642. continue;
  643. again:
  644. // FIXME these should stop being context-global variables
  645. h->nal_ref_idc = nal->ref_idc;
  646. h->nal_unit_type = nal->type;
  647. err = 0;
  648. switch (nal->type) {
  649. case H264_NAL_IDR_SLICE:
  650. if ((nal->data[1] & 0xFC) == 0x98) {
  651. av_log(h->avctx, AV_LOG_ERROR, "Invalid inter IDR frame\n");
  652. h->next_outputed_poc = INT_MIN;
  653. ret = -1;
  654. goto end;
  655. }
  656. if(!idr_cleared) {
  657. if (h->current_slice && (avctx->active_thread_type & FF_THREAD_SLICE)) {
  658. av_log(h, AV_LOG_ERROR, "invalid mixed IDR / non IDR frames cannot be decoded in slice multithreading mode\n");
  659. ret = AVERROR_INVALIDDATA;
  660. goto end;
  661. }
  662. idr(h); // FIXME ensure we don't lose some frames if there is reordering
  663. }
  664. idr_cleared = 1;
  665. h->has_recovery_point = 1;
  666. case H264_NAL_SLICE:
  667. sl->gb = nal->gb;
  668. if ((err = ff_h264_decode_slice_header(h, sl, nal)))
  669. break;
  670. if (sl->redundant_pic_count > 0)
  671. break;
  672. if (h->current_slice == 1) {
  673. if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS))
  674. decode_postinit(h, i >= nals_needed);
  675. if (h->avctx->hwaccel &&
  676. (ret = h->avctx->hwaccel->start_frame(h->avctx, buf, buf_size)) < 0)
  677. goto end;
  678. #if FF_API_CAP_VDPAU
  679. if (CONFIG_H264_VDPAU_DECODER &&
  680. h->avctx->codec->capabilities & AV_CODEC_CAP_HWACCEL_VDPAU)
  681. ff_vdpau_h264_picture_start(h);
  682. #endif
  683. }
  684. if (avctx->hwaccel) {
  685. ret = avctx->hwaccel->decode_slice(avctx,
  686. nal->raw_data,
  687. nal->raw_size);
  688. if (ret < 0)
  689. goto end;
  690. #if FF_API_CAP_VDPAU
  691. } else if (CONFIG_H264_VDPAU_DECODER &&
  692. h->avctx->codec->capabilities & AV_CODEC_CAP_HWACCEL_VDPAU) {
  693. ff_vdpau_add_data_chunk(h->cur_pic_ptr->f->data[0],
  694. start_code,
  695. sizeof(start_code));
  696. ff_vdpau_add_data_chunk(h->cur_pic_ptr->f->data[0],
  697. nal->raw_data,
  698. nal->raw_size);
  699. #endif
  700. } else
  701. context_count++;
  702. break;
  703. case H264_NAL_DPA:
  704. case H264_NAL_DPB:
  705. case H264_NAL_DPC:
  706. avpriv_request_sample(avctx, "data partitioning");
  707. break;
  708. case H264_NAL_SEI:
  709. ret = ff_h264_sei_decode(&h->sei, &nal->gb, &h->ps, avctx);
  710. h->has_recovery_point = h->has_recovery_point || h->sei.recovery_point.recovery_frame_cnt != -1;
  711. if (avctx->debug & FF_DEBUG_GREEN_MD)
  712. debug_green_metadata(&h->sei.green_metadata, h->avctx);
  713. #if FF_API_AFD
  714. FF_DISABLE_DEPRECATION_WARNINGS
  715. h->avctx->dtg_active_format = h->sei.afd.active_format_description;
  716. FF_ENABLE_DEPRECATION_WARNINGS
  717. #endif /* FF_API_AFD */
  718. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  719. goto end;
  720. break;
  721. case H264_NAL_SPS: {
  722. GetBitContext tmp_gb = nal->gb;
  723. if (ff_h264_decode_seq_parameter_set(&tmp_gb, avctx, &h->ps, 0) >= 0)
  724. break;
  725. av_log(h->avctx, AV_LOG_DEBUG,
  726. "SPS decoding failure, trying again with the complete NAL\n");
  727. init_get_bits8(&tmp_gb, nal->raw_data + 1, nal->raw_size - 1);
  728. if (ff_h264_decode_seq_parameter_set(&tmp_gb, avctx, &h->ps, 0) >= 0)
  729. break;
  730. ff_h264_decode_seq_parameter_set(&nal->gb, avctx, &h->ps, 1);
  731. break;
  732. }
  733. case H264_NAL_PPS:
  734. ret = ff_h264_decode_picture_parameter_set(&nal->gb, avctx, &h->ps,
  735. nal->size_bits);
  736. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  737. goto end;
  738. break;
  739. case H264_NAL_AUD:
  740. case H264_NAL_END_SEQUENCE:
  741. case H264_NAL_END_STREAM:
  742. case H264_NAL_FILLER_DATA:
  743. case H264_NAL_SPS_EXT:
  744. case H264_NAL_AUXILIARY_SLICE:
  745. break;
  746. default:
  747. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
  748. nal->type, nal->size_bits);
  749. }
  750. if (context_count == h->max_contexts) {
  751. ret = ff_h264_execute_decode_slices(h, context_count);
  752. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  753. goto end;
  754. context_count = 0;
  755. }
  756. if (err < 0 || err == SLICE_SKIPED) {
  757. if (err < 0)
  758. av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  759. sl->ref_count[0] = sl->ref_count[1] = sl->list_count = 0;
  760. } else if (err == SLICE_SINGLETHREAD) {
  761. if (context_count > 0) {
  762. ret = ff_h264_execute_decode_slices(h, context_count);
  763. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  764. goto end;
  765. context_count = 0;
  766. }
  767. /* Slice could not be decoded in parallel mode, restart. */
  768. sl = &h->slice_ctx[0];
  769. goto again;
  770. }
  771. }
  772. if (context_count) {
  773. ret = ff_h264_execute_decode_slices(h, context_count);
  774. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  775. goto end;
  776. }
  777. ret = 0;
  778. end:
  779. #if CONFIG_ERROR_RESILIENCE
  780. /*
  781. * FIXME: Error handling code does not seem to support interlaced
  782. * when slices span multiple rows
  783. * The ff_er_add_slice calls don't work right for bottom
  784. * fields; they cause massive erroneous error concealing
  785. * Error marking covers both fields (top and bottom).
  786. * This causes a mismatched s->error_count
  787. * and a bad error table. Further, the error count goes to
  788. * INT_MAX when called for bottom field, because mb_y is
  789. * past end by one (callers fault) and resync_mb_y != 0
  790. * causes problems for the first MB line, too.
  791. */
  792. if (!FIELD_PICTURE(h) && h->current_slice &&
  793. h->ps.sps == (const SPS*)h->ps.sps_list[h->ps.pps->sps_id]->data &&
  794. h->enable_er) {
  795. H264SliceContext *sl = h->slice_ctx;
  796. int use_last_pic = h->last_pic_for_ec.f->buf[0] && !sl->ref_count[0];
  797. ff_h264_set_erpic(&sl->er.cur_pic, h->cur_pic_ptr);
  798. if (use_last_pic) {
  799. ff_h264_set_erpic(&sl->er.last_pic, &h->last_pic_for_ec);
  800. sl->ref_list[0][0].parent = &h->last_pic_for_ec;
  801. memcpy(sl->ref_list[0][0].data, h->last_pic_for_ec.f->data, sizeof(sl->ref_list[0][0].data));
  802. memcpy(sl->ref_list[0][0].linesize, h->last_pic_for_ec.f->linesize, sizeof(sl->ref_list[0][0].linesize));
  803. sl->ref_list[0][0].reference = h->last_pic_for_ec.reference;
  804. } else if (sl->ref_count[0]) {
  805. ff_h264_set_erpic(&sl->er.last_pic, sl->ref_list[0][0].parent);
  806. } else
  807. ff_h264_set_erpic(&sl->er.last_pic, NULL);
  808. if (sl->ref_count[1])
  809. ff_h264_set_erpic(&sl->er.next_pic, sl->ref_list[1][0].parent);
  810. sl->er.ref_count = sl->ref_count[0];
  811. ff_er_frame_end(&sl->er);
  812. if (use_last_pic)
  813. memset(&sl->ref_list[0][0], 0, sizeof(sl->ref_list[0][0]));
  814. }
  815. #endif /* CONFIG_ERROR_RESILIENCE */
  816. /* clean up */
  817. if (h->cur_pic_ptr && !h->droppable) {
  818. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  819. h->picture_structure == PICT_BOTTOM_FIELD);
  820. }
  821. return (ret < 0) ? ret : buf_size;
  822. }
  823. /**
  824. * Return the number of bytes consumed for building the current frame.
  825. */
  826. static int get_consumed_bytes(int pos, int buf_size)
  827. {
  828. if (pos == 0)
  829. pos = 1; // avoid infinite loops (I doubt that is needed but...)
  830. if (pos + 10 > buf_size)
  831. pos = buf_size; // oops ;)
  832. return pos;
  833. }
  834. static int output_frame(H264Context *h, AVFrame *dst, H264Picture *srcp)
  835. {
  836. AVFrame *src = srcp->f;
  837. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(src->format);
  838. int i;
  839. int ret = av_frame_ref(dst, src);
  840. if (ret < 0)
  841. return ret;
  842. av_dict_set(&dst->metadata, "stereo_mode", ff_h264_sei_stereo_mode(&h->sei.frame_packing), 0);
  843. h->backup_width = h->avctx->width;
  844. h->backup_height = h->avctx->height;
  845. h->backup_pix_fmt = h->avctx->pix_fmt;
  846. h->avctx->width = dst->width;
  847. h->avctx->height = dst->height;
  848. h->avctx->pix_fmt = dst->format;
  849. if (srcp->sei_recovery_frame_cnt == 0)
  850. dst->key_frame = 1;
  851. if (!srcp->crop)
  852. return 0;
  853. for (i = 0; i < desc->nb_components; i++) {
  854. int hshift = (i > 0) ? desc->log2_chroma_w : 0;
  855. int vshift = (i > 0) ? desc->log2_chroma_h : 0;
  856. int off = ((srcp->crop_left >> hshift) << h->pixel_shift) +
  857. (srcp->crop_top >> vshift) * dst->linesize[i];
  858. dst->data[i] += off;
  859. }
  860. return 0;
  861. }
  862. static int is_extra(const uint8_t *buf, int buf_size)
  863. {
  864. int cnt= buf[5]&0x1f;
  865. const uint8_t *p= buf+6;
  866. while(cnt--){
  867. int nalsize= AV_RB16(p) + 2;
  868. if(nalsize > buf_size - (p-buf) || (p[2] & 0x9F) != 7)
  869. return 0;
  870. p += nalsize;
  871. }
  872. cnt = *(p++);
  873. if(!cnt)
  874. return 0;
  875. while(cnt--){
  876. int nalsize= AV_RB16(p) + 2;
  877. if(nalsize > buf_size - (p-buf) || (p[2] & 0x9F) != 8)
  878. return 0;
  879. p += nalsize;
  880. }
  881. return 1;
  882. }
  883. static int finalize_frame(H264Context *h, AVFrame *dst, H264Picture *out, int *got_frame)
  884. {
  885. int ret;
  886. if (((h->avctx->flags & AV_CODEC_FLAG_OUTPUT_CORRUPT) ||
  887. (h->avctx->flags2 & AV_CODEC_FLAG2_SHOW_ALL) ||
  888. out->recovered)) {
  889. if (!out->recovered)
  890. out->f->flags |= AV_FRAME_FLAG_CORRUPT;
  891. if (!h->avctx->hwaccel &&
  892. (out->field_poc[0] == INT_MAX ||
  893. out->field_poc[1] == INT_MAX)
  894. ) {
  895. int p;
  896. AVFrame *f = out->f;
  897. int field = out->field_poc[0] == INT_MAX;
  898. uint8_t *dst_data[4];
  899. int linesizes[4];
  900. const uint8_t *src_data[4];
  901. av_log(h->avctx, AV_LOG_DEBUG, "Duplicating field %d to fill missing\n", field);
  902. for (p = 0; p<4; p++) {
  903. dst_data[p] = f->data[p] + (field^1)*f->linesize[p];
  904. src_data[p] = f->data[p] + field *f->linesize[p];
  905. linesizes[p] = 2*f->linesize[p];
  906. }
  907. av_image_copy(dst_data, linesizes, src_data, linesizes,
  908. f->format, f->width, f->height>>1);
  909. }
  910. ret = output_frame(h, dst, out);
  911. if (ret < 0)
  912. return ret;
  913. *got_frame = 1;
  914. if (CONFIG_MPEGVIDEO) {
  915. ff_print_debug_info2(h->avctx, dst, NULL,
  916. out->mb_type,
  917. out->qscale_table,
  918. out->motion_val,
  919. NULL,
  920. h->mb_width, h->mb_height, h->mb_stride, 1);
  921. }
  922. }
  923. return 0;
  924. }
  925. static int h264_decode_frame(AVCodecContext *avctx, void *data,
  926. int *got_frame, AVPacket *avpkt)
  927. {
  928. const uint8_t *buf = avpkt->data;
  929. int buf_size = avpkt->size;
  930. H264Context *h = avctx->priv_data;
  931. AVFrame *pict = data;
  932. int buf_index = 0;
  933. H264Picture *out;
  934. int i, out_idx;
  935. int ret;
  936. h->flags = avctx->flags;
  937. h->setup_finished = 0;
  938. if (h->backup_width != -1) {
  939. avctx->width = h->backup_width;
  940. h->backup_width = -1;
  941. }
  942. if (h->backup_height != -1) {
  943. avctx->height = h->backup_height;
  944. h->backup_height = -1;
  945. }
  946. if (h->backup_pix_fmt != AV_PIX_FMT_NONE) {
  947. avctx->pix_fmt = h->backup_pix_fmt;
  948. h->backup_pix_fmt = AV_PIX_FMT_NONE;
  949. }
  950. ff_h264_unref_picture(h, &h->last_pic_for_ec);
  951. /* end of stream, output what is still in the buffers */
  952. if (buf_size == 0) {
  953. out:
  954. h->cur_pic_ptr = NULL;
  955. h->first_field = 0;
  956. out = h->delayed_pic[0];
  957. out_idx = 0;
  958. for (i = 1;
  959. h->delayed_pic[i] &&
  960. !h->delayed_pic[i]->f->key_frame &&
  961. !h->delayed_pic[i]->mmco_reset;
  962. i++)
  963. if (h->delayed_pic[i]->poc < out->poc) {
  964. out = h->delayed_pic[i];
  965. out_idx = i;
  966. }
  967. for (i = out_idx; h->delayed_pic[i]; i++)
  968. h->delayed_pic[i] = h->delayed_pic[i + 1];
  969. if (out) {
  970. out->reference &= ~DELAYED_PIC_REF;
  971. ret = finalize_frame(h, pict, out, got_frame);
  972. if (ret < 0)
  973. return ret;
  974. }
  975. return buf_index;
  976. }
  977. if (h->is_avc && av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, NULL)) {
  978. int side_size;
  979. uint8_t *side = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, &side_size);
  980. if (is_extra(side, side_size))
  981. ff_h264_decode_extradata(side, side_size,
  982. &h->ps, &h->is_avc, &h->nal_length_size,
  983. avctx->err_recognition, avctx);
  984. }
  985. if(h->is_avc && buf_size >= 9 && buf[0]==1 && buf[2]==0 && (buf[4]&0xFC)==0xFC && (buf[5]&0x1F) && buf[8]==0x67){
  986. if (is_extra(buf, buf_size))
  987. return ff_h264_decode_extradata(buf, buf_size,
  988. &h->ps, &h->is_avc, &h->nal_length_size,
  989. avctx->err_recognition, avctx);
  990. }
  991. buf_index = decode_nal_units(h, buf, buf_size);
  992. if (buf_index < 0)
  993. return AVERROR_INVALIDDATA;
  994. if (!h->cur_pic_ptr && h->nal_unit_type == H264_NAL_END_SEQUENCE) {
  995. av_assert0(buf_index <= buf_size);
  996. goto out;
  997. }
  998. if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) && !h->cur_pic_ptr) {
  999. if (avctx->skip_frame >= AVDISCARD_NONREF ||
  1000. buf_size >= 4 && !memcmp("Q264", buf, 4))
  1001. return buf_size;
  1002. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  1003. return AVERROR_INVALIDDATA;
  1004. }
  1005. if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) ||
  1006. (h->mb_y >= h->mb_height && h->mb_height)) {
  1007. if (avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)
  1008. decode_postinit(h, 1);
  1009. if ((ret = ff_h264_field_end(h, &h->slice_ctx[0], 0)) < 0)
  1010. return ret;
  1011. /* Wait for second field. */
  1012. if (h->next_output_pic) {
  1013. ret = finalize_frame(h, pict, h->next_output_pic, got_frame);
  1014. if (ret < 0)
  1015. return ret;
  1016. }
  1017. }
  1018. av_assert0(pict->buf[0] || !*got_frame);
  1019. ff_h264_unref_picture(h, &h->last_pic_for_ec);
  1020. return get_consumed_bytes(buf_index, buf_size);
  1021. }
  1022. #define OFFSET(x) offsetof(H264Context, x)
  1023. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1024. static const AVOption h264_options[] = {
  1025. {"is_avc", "is avc", offsetof(H264Context, is_avc), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, 0},
  1026. {"nal_length_size", "nal_length_size", offsetof(H264Context, nal_length_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 4, 0},
  1027. { "enable_er", "Enable error resilience on damaged frames (unsafe)", OFFSET(enable_er), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VD },
  1028. { NULL },
  1029. };
  1030. static const AVClass h264_class = {
  1031. .class_name = "H264 Decoder",
  1032. .item_name = av_default_item_name,
  1033. .option = h264_options,
  1034. .version = LIBAVUTIL_VERSION_INT,
  1035. };
  1036. AVCodec ff_h264_decoder = {
  1037. .name = "h264",
  1038. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  1039. .type = AVMEDIA_TYPE_VIDEO,
  1040. .id = AV_CODEC_ID_H264,
  1041. .priv_data_size = sizeof(H264Context),
  1042. .init = ff_h264_decode_init,
  1043. .close = h264_decode_end,
  1044. .decode = h264_decode_frame,
  1045. .capabilities = /*AV_CODEC_CAP_DRAW_HORIZ_BAND |*/ AV_CODEC_CAP_DR1 |
  1046. AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS |
  1047. AV_CODEC_CAP_FRAME_THREADS,
  1048. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  1049. .flush = flush_dpb,
  1050. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  1051. .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_h264_update_thread_context),
  1052. .profiles = NULL_IF_CONFIG_SMALL(ff_h264_profiles),
  1053. .priv_class = &h264_class,
  1054. };
  1055. #if CONFIG_H264_VDPAU_DECODER && FF_API_VDPAU
  1056. static const AVClass h264_vdpau_class = {
  1057. .class_name = "H264 VDPAU Decoder",
  1058. .item_name = av_default_item_name,
  1059. .option = h264_options,
  1060. .version = LIBAVUTIL_VERSION_INT,
  1061. };
  1062. AVCodec ff_h264_vdpau_decoder = {
  1063. .name = "h264_vdpau",
  1064. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (VDPAU acceleration)"),
  1065. .type = AVMEDIA_TYPE_VIDEO,
  1066. .id = AV_CODEC_ID_H264,
  1067. .priv_data_size = sizeof(H264Context),
  1068. .init = ff_h264_decode_init,
  1069. .close = h264_decode_end,
  1070. .decode = h264_decode_frame,
  1071. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HWACCEL_VDPAU,
  1072. .flush = flush_dpb,
  1073. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_VDPAU_H264,
  1074. AV_PIX_FMT_NONE},
  1075. .profiles = NULL_IF_CONFIG_SMALL(ff_h264_profiles),
  1076. .priv_class = &h264_vdpau_class,
  1077. };
  1078. #endif