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.

795 lines
25KB

  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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. #include "libavutil/display.h"
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/stereo3d.h"
  30. #include "libavutil/timer.h"
  31. #include "internal.h"
  32. #include "bytestream.h"
  33. #include "cabac.h"
  34. #include "cabac_functions.h"
  35. #include "error_resilience.h"
  36. #include "avcodec.h"
  37. #include "golomb_legacy.h"
  38. #include "h264.h"
  39. #include "h264dec.h"
  40. #include "h2645_parse.h"
  41. #include "h264data.h"
  42. #include "h264chroma.h"
  43. #include "h264_mvpred.h"
  44. #include "h264_ps.h"
  45. #include "mathops.h"
  46. #include "me_cmp.h"
  47. #include "mpegutils.h"
  48. #include "profiles.h"
  49. #include "rectangle.h"
  50. #include "thread.h"
  51. #include <assert.h>
  52. const uint16_t ff_h264_mb_sizes[4] = { 256, 384, 512, 768 };
  53. static void h264_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
  54. int (*mv)[2][4][2],
  55. int mb_x, int mb_y, int mb_intra, int mb_skipped)
  56. {
  57. H264Context *h = opaque;
  58. H264SliceContext *sl = &h->slice_ctx[0];
  59. sl->mb_x = mb_x;
  60. sl->mb_y = mb_y;
  61. sl->mb_xy = mb_x + mb_y * h->mb_stride;
  62. memset(sl->non_zero_count_cache, 0, sizeof(sl->non_zero_count_cache));
  63. assert(ref >= 0);
  64. /* FIXME: It is possible albeit uncommon that slice references
  65. * differ between slices. We take the easy approach and ignore
  66. * it for now. If this turns out to have any relevance in
  67. * practice then correct remapping should be added. */
  68. if (ref >= sl->ref_count[0])
  69. ref = 0;
  70. fill_rectangle(&h->cur_pic.ref_index[0][4 * sl->mb_xy],
  71. 2, 2, 2, ref, 1);
  72. fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
  73. fill_rectangle(sl->mv_cache[0][scan8[0]], 4, 4, 8,
  74. pack16to32((*mv)[0][0][0], (*mv)[0][0][1]), 4);
  75. assert(!FRAME_MBAFF(h));
  76. ff_h264_hl_decode_mb(h, &h->slice_ctx[0]);
  77. }
  78. void ff_h264_draw_horiz_band(const H264Context *h, H264SliceContext *sl,
  79. int y, int height)
  80. {
  81. AVCodecContext *avctx = h->avctx;
  82. const AVFrame *src = h->cur_pic.f;
  83. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  84. int vshift = desc->log2_chroma_h;
  85. const int field_pic = h->picture_structure != PICT_FRAME;
  86. if (field_pic) {
  87. height <<= 1;
  88. y <<= 1;
  89. }
  90. height = FFMIN(height, avctx->height - y);
  91. if (field_pic && h->first_field && !(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD))
  92. return;
  93. if (avctx->draw_horiz_band) {
  94. int offset[AV_NUM_DATA_POINTERS];
  95. int i;
  96. offset[0] = y * src->linesize[0];
  97. offset[1] =
  98. offset[2] = (y >> vshift) * src->linesize[1];
  99. for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
  100. offset[i] = 0;
  101. emms_c();
  102. avctx->draw_horiz_band(avctx, src, offset,
  103. y, h->picture_structure, height);
  104. }
  105. }
  106. void ff_h264_free_tables(H264Context *h)
  107. {
  108. int i;
  109. av_freep(&h->intra4x4_pred_mode);
  110. av_freep(&h->chroma_pred_mode_table);
  111. av_freep(&h->cbp_table);
  112. av_freep(&h->mvd_table[0]);
  113. av_freep(&h->mvd_table[1]);
  114. av_freep(&h->direct_table);
  115. av_freep(&h->non_zero_count);
  116. av_freep(&h->slice_table_base);
  117. h->slice_table = NULL;
  118. av_freep(&h->list_counts);
  119. av_freep(&h->mb2b_xy);
  120. av_freep(&h->mb2br_xy);
  121. av_buffer_pool_uninit(&h->qscale_table_pool);
  122. av_buffer_pool_uninit(&h->mb_type_pool);
  123. av_buffer_pool_uninit(&h->motion_val_pool);
  124. av_buffer_pool_uninit(&h->ref_index_pool);
  125. for (i = 0; i < h->nb_slice_ctx; i++) {
  126. H264SliceContext *sl = &h->slice_ctx[i];
  127. av_freep(&sl->dc_val_base);
  128. av_freep(&sl->er.mb_index2xy);
  129. av_freep(&sl->er.error_status_table);
  130. av_freep(&sl->er.er_temp_buffer);
  131. av_freep(&sl->bipred_scratchpad);
  132. av_freep(&sl->edge_emu_buffer);
  133. av_freep(&sl->top_borders[0]);
  134. av_freep(&sl->top_borders[1]);
  135. sl->bipred_scratchpad_allocated = 0;
  136. sl->edge_emu_buffer_allocated = 0;
  137. sl->top_borders_allocated[0] = 0;
  138. sl->top_borders_allocated[1] = 0;
  139. }
  140. }
  141. int ff_h264_alloc_tables(H264Context *h)
  142. {
  143. const int big_mb_num = h->mb_stride * (h->mb_height + 1);
  144. const int row_mb_num = h->mb_stride * 2 * h->nb_slice_ctx;
  145. int x, y;
  146. FF_ALLOCZ_OR_GOTO(h->avctx, h->intra4x4_pred_mode,
  147. row_mb_num * 8 * sizeof(uint8_t), fail)
  148. h->slice_ctx[0].intra4x4_pred_mode = h->intra4x4_pred_mode;
  149. FF_ALLOCZ_OR_GOTO(h->avctx, h->non_zero_count,
  150. big_mb_num * 48 * sizeof(uint8_t), fail)
  151. FF_ALLOCZ_OR_GOTO(h->avctx, h->slice_table_base,
  152. (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base), fail)
  153. FF_ALLOCZ_OR_GOTO(h->avctx, h->cbp_table,
  154. big_mb_num * sizeof(uint16_t), fail)
  155. FF_ALLOCZ_OR_GOTO(h->avctx, h->chroma_pred_mode_table,
  156. big_mb_num * sizeof(uint8_t), fail)
  157. FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[0],
  158. 16 * row_mb_num * sizeof(uint8_t), fail);
  159. FF_ALLOCZ_OR_GOTO(h->avctx, h->mvd_table[1],
  160. 16 * row_mb_num * sizeof(uint8_t), fail);
  161. h->slice_ctx[0].mvd_table[0] = h->mvd_table[0];
  162. h->slice_ctx[0].mvd_table[1] = h->mvd_table[1];
  163. FF_ALLOCZ_OR_GOTO(h->avctx, h->direct_table,
  164. 4 * big_mb_num * sizeof(uint8_t), fail);
  165. FF_ALLOCZ_OR_GOTO(h->avctx, h->list_counts,
  166. big_mb_num * sizeof(uint8_t), fail)
  167. memset(h->slice_table_base, -1,
  168. (big_mb_num + h->mb_stride) * sizeof(*h->slice_table_base));
  169. h->slice_table = h->slice_table_base + h->mb_stride * 2 + 1;
  170. FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2b_xy,
  171. big_mb_num * sizeof(uint32_t), fail);
  172. FF_ALLOCZ_OR_GOTO(h->avctx, h->mb2br_xy,
  173. big_mb_num * sizeof(uint32_t), fail);
  174. for (y = 0; y < h->mb_height; y++)
  175. for (x = 0; x < h->mb_width; x++) {
  176. const int mb_xy = x + y * h->mb_stride;
  177. const int b_xy = 4 * x + 4 * y * h->b_stride;
  178. h->mb2b_xy[mb_xy] = b_xy;
  179. h->mb2br_xy[mb_xy] = 8 * (FMO ? mb_xy : (mb_xy % (2 * h->mb_stride)));
  180. }
  181. return 0;
  182. fail:
  183. ff_h264_free_tables(h);
  184. return AVERROR(ENOMEM);
  185. }
  186. /**
  187. * Init context
  188. * Allocate buffers which are not shared amongst multiple threads.
  189. */
  190. int ff_h264_slice_context_init(H264Context *h, H264SliceContext *sl)
  191. {
  192. ERContext *er = &sl->er;
  193. int mb_array_size = h->mb_height * h->mb_stride;
  194. int y_size = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
  195. int c_size = h->mb_stride * (h->mb_height + 1);
  196. int yc_size = y_size + 2 * c_size;
  197. int x, y, i;
  198. sl->ref_cache[0][scan8[5] + 1] =
  199. sl->ref_cache[0][scan8[7] + 1] =
  200. sl->ref_cache[0][scan8[13] + 1] =
  201. sl->ref_cache[1][scan8[5] + 1] =
  202. sl->ref_cache[1][scan8[7] + 1] =
  203. sl->ref_cache[1][scan8[13] + 1] = PART_NOT_AVAILABLE;
  204. if (CONFIG_ERROR_RESILIENCE) {
  205. /* init ER */
  206. er->avctx = h->avctx;
  207. er->decode_mb = h264_er_decode_mb;
  208. er->opaque = h;
  209. er->quarter_sample = 1;
  210. er->mb_num = h->mb_num;
  211. er->mb_width = h->mb_width;
  212. er->mb_height = h->mb_height;
  213. er->mb_stride = h->mb_stride;
  214. er->b8_stride = h->mb_width * 2 + 1;
  215. // error resilience code looks cleaner with this
  216. FF_ALLOCZ_OR_GOTO(h->avctx, er->mb_index2xy,
  217. (h->mb_num + 1) * sizeof(int), fail);
  218. for (y = 0; y < h->mb_height; y++)
  219. for (x = 0; x < h->mb_width; x++)
  220. er->mb_index2xy[x + y * h->mb_width] = x + y * h->mb_stride;
  221. er->mb_index2xy[h->mb_height * h->mb_width] = (h->mb_height - 1) *
  222. h->mb_stride + h->mb_width;
  223. FF_ALLOCZ_OR_GOTO(h->avctx, er->error_status_table,
  224. mb_array_size * sizeof(uint8_t), fail);
  225. FF_ALLOC_OR_GOTO(h->avctx, er->er_temp_buffer,
  226. h->mb_height * h->mb_stride, fail);
  227. FF_ALLOCZ_OR_GOTO(h->avctx, sl->dc_val_base,
  228. yc_size * sizeof(int16_t), fail);
  229. er->dc_val[0] = sl->dc_val_base + h->mb_width * 2 + 2;
  230. er->dc_val[1] = sl->dc_val_base + y_size + h->mb_stride + 1;
  231. er->dc_val[2] = er->dc_val[1] + c_size;
  232. for (i = 0; i < yc_size; i++)
  233. sl->dc_val_base[i] = 1024;
  234. }
  235. return 0;
  236. fail:
  237. return AVERROR(ENOMEM); // ff_h264_free_tables will clean up for us
  238. }
  239. static int h264_init_context(AVCodecContext *avctx, H264Context *h)
  240. {
  241. int i;
  242. h->avctx = avctx;
  243. h->width_from_caller = avctx->width;
  244. h->height_from_caller = avctx->height;
  245. h->picture_structure = PICT_FRAME;
  246. h->workaround_bugs = avctx->workaround_bugs;
  247. h->flags = avctx->flags;
  248. h->poc.prev_poc_msb = 1 << 16;
  249. h->recovery_frame = -1;
  250. h->frame_recovered = 0;
  251. h->next_outputed_poc = INT_MIN;
  252. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  253. h->last_pocs[i] = INT_MIN;
  254. ff_h264_sei_uninit(&h->sei);
  255. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  256. h->nb_slice_ctx = (avctx->active_thread_type & FF_THREAD_SLICE) ? avctx->thread_count : 1;
  257. h->slice_ctx = av_mallocz_array(h->nb_slice_ctx, sizeof(*h->slice_ctx));
  258. if (!h->slice_ctx) {
  259. h->nb_slice_ctx = 0;
  260. return AVERROR(ENOMEM);
  261. }
  262. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
  263. h->DPB[i].f = av_frame_alloc();
  264. if (!h->DPB[i].f)
  265. return AVERROR(ENOMEM);
  266. }
  267. h->cur_pic.f = av_frame_alloc();
  268. if (!h->cur_pic.f)
  269. return AVERROR(ENOMEM);
  270. h->output_frame = av_frame_alloc();
  271. if (!h->output_frame)
  272. return AVERROR(ENOMEM);
  273. for (i = 0; i < h->nb_slice_ctx; i++)
  274. h->slice_ctx[i].h264 = h;
  275. return 0;
  276. }
  277. static av_cold int h264_decode_end(AVCodecContext *avctx)
  278. {
  279. H264Context *h = avctx->priv_data;
  280. int i;
  281. ff_h264_free_tables(h);
  282. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++) {
  283. ff_h264_unref_picture(h, &h->DPB[i]);
  284. av_frame_free(&h->DPB[i].f);
  285. }
  286. h->cur_pic_ptr = NULL;
  287. av_freep(&h->slice_ctx);
  288. h->nb_slice_ctx = 0;
  289. for (i = 0; i < MAX_SPS_COUNT; i++)
  290. av_buffer_unref(&h->ps.sps_list[i]);
  291. for (i = 0; i < MAX_PPS_COUNT; i++)
  292. av_buffer_unref(&h->ps.pps_list[i]);
  293. ff_h2645_packet_uninit(&h->pkt);
  294. ff_h264_unref_picture(h, &h->cur_pic);
  295. av_frame_free(&h->cur_pic.f);
  296. av_frame_free(&h->output_frame);
  297. return 0;
  298. }
  299. static AVOnce h264_vlc_init = AV_ONCE_INIT;
  300. static av_cold int h264_decode_init(AVCodecContext *avctx)
  301. {
  302. H264Context *h = avctx->priv_data;
  303. int ret;
  304. ret = h264_init_context(avctx, h);
  305. if (ret < 0)
  306. return ret;
  307. ret = ff_thread_once(&h264_vlc_init, ff_h264_decode_init_vlc);
  308. if (ret != 0) {
  309. av_log(avctx, AV_LOG_ERROR, "pthread_once has failed.");
  310. return AVERROR_UNKNOWN;
  311. }
  312. if (avctx->ticks_per_frame == 1)
  313. h->avctx->framerate.num *= 2;
  314. avctx->ticks_per_frame = 2;
  315. if (avctx->extradata_size > 0 && avctx->extradata) {
  316. ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
  317. &h->ps, &h->is_avc, &h->nal_length_size,
  318. avctx->err_recognition, avctx);
  319. if (ret < 0) {
  320. h264_decode_end(avctx);
  321. return ret;
  322. }
  323. }
  324. if (h->ps.sps && h->ps.sps->bitstream_restriction_flag &&
  325. h->avctx->has_b_frames < h->ps.sps->num_reorder_frames) {
  326. h->avctx->has_b_frames = h->ps.sps->num_reorder_frames;
  327. }
  328. avctx->internal->allocate_progress = 1;
  329. if (h->enable_er) {
  330. av_log(avctx, AV_LOG_WARNING,
  331. "Error resilience is enabled. It is unsafe and unsupported and may crash. "
  332. "Use it at your own risk\n");
  333. }
  334. return 0;
  335. }
  336. static int decode_init_thread_copy(AVCodecContext *avctx)
  337. {
  338. H264Context *h = avctx->priv_data;
  339. int ret;
  340. if (!avctx->internal->is_copy)
  341. return 0;
  342. memset(h, 0, sizeof(*h));
  343. ret = h264_init_context(avctx, h);
  344. if (ret < 0)
  345. return ret;
  346. h->context_initialized = 0;
  347. return 0;
  348. }
  349. /**
  350. * instantaneous decoder refresh.
  351. */
  352. static void idr(H264Context *h)
  353. {
  354. ff_h264_remove_all_refs(h);
  355. h->poc.prev_frame_num =
  356. h->poc.prev_frame_num_offset =
  357. h->poc.prev_poc_msb =
  358. h->poc.prev_poc_lsb = 0;
  359. }
  360. /* forget old pics after a seek */
  361. void ff_h264_flush_change(H264Context *h)
  362. {
  363. int i;
  364. for (i = 0; i < MAX_DELAYED_PIC_COUNT; i++)
  365. h->last_pocs[i] = INT_MIN;
  366. h->next_outputed_poc = INT_MIN;
  367. h->prev_interlaced_frame = 1;
  368. idr(h);
  369. if (h->cur_pic_ptr)
  370. h->cur_pic_ptr->reference = 0;
  371. h->first_field = 0;
  372. h->recovery_frame = -1;
  373. h->frame_recovered = 0;
  374. }
  375. /* forget old pics after a seek */
  376. static void flush_dpb(AVCodecContext *avctx)
  377. {
  378. H264Context *h = avctx->priv_data;
  379. int i;
  380. memset(h->delayed_pic, 0, sizeof(h->delayed_pic));
  381. ff_h264_flush_change(h);
  382. ff_h264_sei_uninit(&h->sei);
  383. for (i = 0; i < H264_MAX_PICTURE_COUNT; i++)
  384. ff_h264_unref_picture(h, &h->DPB[i]);
  385. h->cur_pic_ptr = NULL;
  386. ff_h264_unref_picture(h, &h->cur_pic);
  387. h->mb_y = 0;
  388. ff_h264_free_tables(h);
  389. h->context_initialized = 0;
  390. }
  391. static int get_last_needed_nal(H264Context *h)
  392. {
  393. int nals_needed = 0;
  394. int i, ret;
  395. for (i = 0; i < h->pkt.nb_nals; i++) {
  396. H2645NAL *nal = &h->pkt.nals[i];
  397. GetBitContext gb;
  398. /* packets can sometimes contain multiple PPS/SPS,
  399. * e.g. two PAFF field pictures in one packet, or a demuxer
  400. * which splits NALs strangely if so, when frame threading we
  401. * can't start the next thread until we've read all of them */
  402. switch (nal->type) {
  403. case H264_NAL_SPS:
  404. case H264_NAL_PPS:
  405. nals_needed = i;
  406. break;
  407. case H264_NAL_DPA:
  408. case H264_NAL_IDR_SLICE:
  409. case H264_NAL_SLICE:
  410. ret = init_get_bits8(&gb, nal->data + 1, nal->size - 1);
  411. if (ret < 0) {
  412. av_log(h->avctx, AV_LOG_ERROR, "Invalid zero-sized VCL NAL unit\n");
  413. if (h->avctx->err_recognition & AV_EF_EXPLODE)
  414. return ret;
  415. break;
  416. }
  417. if (!get_ue_golomb(&gb))
  418. nals_needed = i;
  419. }
  420. }
  421. return nals_needed;
  422. }
  423. static int decode_nal_units(H264Context *h, const uint8_t *buf, int buf_size)
  424. {
  425. AVCodecContext *const avctx = h->avctx;
  426. int nals_needed = 0; ///< number of NALs that need decoding before the next frame thread starts
  427. int i, ret = 0;
  428. if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS)) {
  429. h->current_slice = 0;
  430. h->field_started = 0;
  431. if (!h->first_field)
  432. h->cur_pic_ptr = NULL;
  433. ff_h264_sei_uninit(&h->sei);
  434. }
  435. ret = ff_h2645_packet_split(&h->pkt, buf, buf_size, avctx, h->is_avc,
  436. h->nal_length_size, avctx->codec_id);
  437. if (ret < 0) {
  438. av_log(avctx, AV_LOG_ERROR,
  439. "Error splitting the input into NAL units.\n");
  440. /* There are samples in the wild with mp4-style extradata, but Annex B
  441. * data in the packets. If we fail parsing the packet as mp4, try it again
  442. * as Annex B. */
  443. if (h->is_avc && !(avctx->err_recognition & AV_EF_EXPLODE)) {
  444. int err = ff_h2645_packet_split(&h->pkt, buf, buf_size, avctx, 0, 0,
  445. avctx->codec_id);
  446. if (err >= 0) {
  447. av_log(avctx, AV_LOG_WARNING,
  448. "The stream seems to contain AVCC extradata with Annex B "
  449. "formatted data, which is invalid.");
  450. h->is_avc = 0;
  451. ret = 0;
  452. }
  453. }
  454. if (ret < 0)
  455. return ret;
  456. }
  457. if (avctx->active_thread_type & FF_THREAD_FRAME)
  458. nals_needed = get_last_needed_nal(h);
  459. for (i = 0; i < h->pkt.nb_nals; i++) {
  460. H2645NAL *nal = &h->pkt.nals[i];
  461. int max_slice_ctx, err;
  462. if (avctx->skip_frame >= AVDISCARD_NONREF &&
  463. nal->ref_idc == 0 && nal->type != H264_NAL_SEI)
  464. continue;
  465. // FIXME these should stop being context-global variables
  466. h->nal_ref_idc = nal->ref_idc;
  467. h->nal_unit_type = nal->type;
  468. err = 0;
  469. switch (nal->type) {
  470. case H264_NAL_IDR_SLICE:
  471. idr(h); // FIXME ensure we don't lose some frames if there is reordering
  472. case H264_NAL_SLICE:
  473. if ((err = ff_h264_queue_decode_slice(h, nal)))
  474. break;
  475. if (avctx->active_thread_type & FF_THREAD_FRAME &&
  476. i >= nals_needed && !h->setup_finished && h->cur_pic_ptr) {
  477. ff_thread_finish_setup(avctx);
  478. h->setup_finished = 1;
  479. }
  480. max_slice_ctx = avctx->hwaccel ? 1 : h->nb_slice_ctx;
  481. if (h->nb_slice_ctx_queued == max_slice_ctx) {
  482. if (avctx->hwaccel) {
  483. ret = avctx->hwaccel->decode_slice(avctx, nal->raw_data, nal->raw_size);
  484. h->nb_slice_ctx_queued = 0;
  485. } else
  486. ret = ff_h264_execute_decode_slices(h);
  487. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  488. goto end;
  489. }
  490. break;
  491. case H264_NAL_DPA:
  492. case H264_NAL_DPB:
  493. case H264_NAL_DPC:
  494. avpriv_request_sample(avctx, "data partitioning");
  495. ret = AVERROR(ENOSYS);
  496. goto end;
  497. break;
  498. case H264_NAL_SEI:
  499. ret = ff_h264_sei_decode(&h->sei, &nal->gb, &h->ps, avctx);
  500. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  501. goto end;
  502. break;
  503. case H264_NAL_SPS:
  504. ret = ff_h264_decode_seq_parameter_set(&nal->gb, avctx, &h->ps);
  505. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  506. goto end;
  507. break;
  508. case H264_NAL_PPS:
  509. ret = ff_h264_decode_picture_parameter_set(&nal->gb, avctx, &h->ps,
  510. nal->size_bits);
  511. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  512. goto end;
  513. break;
  514. case H264_NAL_AUD:
  515. case H264_NAL_END_SEQUENCE:
  516. case H264_NAL_END_STREAM:
  517. case H264_NAL_FILLER_DATA:
  518. case H264_NAL_SPS_EXT:
  519. case H264_NAL_AUXILIARY_SLICE:
  520. break;
  521. default:
  522. av_log(avctx, AV_LOG_DEBUG, "Unknown NAL code: %d (%d bits)\n",
  523. nal->type, nal->size_bits);
  524. }
  525. if (err < 0) {
  526. av_log(h->avctx, AV_LOG_ERROR, "decode_slice_header error\n");
  527. }
  528. }
  529. ret = ff_h264_execute_decode_slices(h);
  530. if (ret < 0 && (h->avctx->err_recognition & AV_EF_EXPLODE))
  531. goto end;
  532. ret = 0;
  533. end:
  534. /* clean up */
  535. if (h->cur_pic_ptr && !h->droppable) {
  536. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  537. h->picture_structure == PICT_BOTTOM_FIELD);
  538. }
  539. return (ret < 0) ? ret : buf_size;
  540. }
  541. /**
  542. * Return the number of bytes consumed for building the current frame.
  543. */
  544. static int get_consumed_bytes(int pos, int buf_size)
  545. {
  546. if (pos == 0)
  547. pos = 1; // avoid infinite loops (I doubt that is needed but...)
  548. if (pos + 10 > buf_size)
  549. pos = buf_size; // oops ;)
  550. return pos;
  551. }
  552. static int h264_decode_frame(AVCodecContext *avctx, void *data,
  553. int *got_frame, AVPacket *avpkt)
  554. {
  555. const uint8_t *buf = avpkt->data;
  556. int buf_size = avpkt->size;
  557. H264Context *h = avctx->priv_data;
  558. AVFrame *pict = data;
  559. int buf_index = 0;
  560. int ret;
  561. const uint8_t *new_extradata;
  562. int new_extradata_size;
  563. h->flags = avctx->flags;
  564. h->setup_finished = 0;
  565. h->nb_slice_ctx_queued = 0;
  566. /* end of stream, output what is still in the buffers */
  567. out:
  568. if (buf_size == 0) {
  569. H264Picture *out;
  570. int i, out_idx;
  571. h->cur_pic_ptr = NULL;
  572. // FIXME factorize this with the output code below
  573. out = h->delayed_pic[0];
  574. out_idx = 0;
  575. for (i = 1;
  576. h->delayed_pic[i] &&
  577. !h->delayed_pic[i]->f->key_frame &&
  578. !h->delayed_pic[i]->mmco_reset;
  579. i++)
  580. if (h->delayed_pic[i]->poc < out->poc) {
  581. out = h->delayed_pic[i];
  582. out_idx = i;
  583. }
  584. for (i = out_idx; h->delayed_pic[i]; i++)
  585. h->delayed_pic[i] = h->delayed_pic[i + 1];
  586. if (out) {
  587. ret = av_frame_ref(pict, out->f);
  588. if (ret < 0)
  589. return ret;
  590. *got_frame = 1;
  591. }
  592. return buf_index;
  593. }
  594. new_extradata_size = 0;
  595. new_extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
  596. &new_extradata_size);
  597. if (new_extradata_size > 0 && new_extradata) {
  598. ret = ff_h264_decode_extradata(new_extradata, new_extradata_size,
  599. &h->ps, &h->is_avc, &h->nal_length_size,
  600. avctx->err_recognition, avctx);
  601. if (ret < 0)
  602. return ret;
  603. }
  604. buf_index = decode_nal_units(h, buf, buf_size);
  605. if (buf_index < 0)
  606. return AVERROR_INVALIDDATA;
  607. if (!h->cur_pic_ptr && h->nal_unit_type == H264_NAL_END_SEQUENCE) {
  608. buf_size = 0;
  609. goto out;
  610. }
  611. if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) && !h->cur_pic_ptr) {
  612. if (avctx->skip_frame >= AVDISCARD_NONREF)
  613. return 0;
  614. av_log(avctx, AV_LOG_ERROR, "no frame!\n");
  615. return AVERROR_INVALIDDATA;
  616. }
  617. if (!(avctx->flags2 & AV_CODEC_FLAG2_CHUNKS) ||
  618. (h->mb_y >= h->mb_height && h->mb_height)) {
  619. if (h->field_started)
  620. ff_h264_field_end(h, &h->slice_ctx[0], 0);
  621. *got_frame = 0;
  622. if (h->output_frame->buf[0]) {
  623. ret = av_frame_ref(pict, h->output_frame);
  624. av_frame_unref(h->output_frame);
  625. if (ret < 0)
  626. return ret;
  627. *got_frame = 1;
  628. }
  629. }
  630. assert(pict->buf[0] || !*got_frame);
  631. return get_consumed_bytes(buf_index, buf_size);
  632. }
  633. #define OFFSET(x) offsetof(H264Context, x)
  634. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  635. static const AVOption h264_options[] = {
  636. { "enable_er", "Enable error resilience on damaged frames (unsafe)", OFFSET(enable_er), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VD },
  637. { NULL },
  638. };
  639. static const AVClass h264_class = {
  640. .class_name = "h264",
  641. .item_name = av_default_item_name,
  642. .option = h264_options,
  643. .version = LIBAVUTIL_VERSION_INT,
  644. };
  645. AVCodec ff_h264_decoder = {
  646. .name = "h264",
  647. .long_name = NULL_IF_CONFIG_SMALL("H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10"),
  648. .type = AVMEDIA_TYPE_VIDEO,
  649. .id = AV_CODEC_ID_H264,
  650. .priv_data_size = sizeof(H264Context),
  651. .init = h264_decode_init,
  652. .close = h264_decode_end,
  653. .decode = h264_decode_frame,
  654. .capabilities = /*AV_CODEC_CAP_DRAW_HORIZ_BAND |*/ AV_CODEC_CAP_DR1 |
  655. AV_CODEC_CAP_DELAY | AV_CODEC_CAP_SLICE_THREADS |
  656. AV_CODEC_CAP_FRAME_THREADS,
  657. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_EXPORTS_CROPPING,
  658. .flush = flush_dpb,
  659. .init_thread_copy = ONLY_IF_THREADS_ENABLED(decode_init_thread_copy),
  660. .update_thread_context = ONLY_IF_THREADS_ENABLED(ff_h264_update_thread_context),
  661. .profiles = NULL_IF_CONFIG_SMALL(ff_h264_profiles),
  662. .priv_class = &h264_class,
  663. };