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.

198 lines
5.7KB

  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. #include "libavutil/avassert.h"
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/timer.h"
  29. #include "internal.h"
  30. #include "cabac.h"
  31. #include "cabac_functions.h"
  32. #include "error_resilience.h"
  33. #include "avcodec.h"
  34. #include "h264.h"
  35. #include "h264data.h"
  36. #include "h264chroma.h"
  37. #include "h264_mvpred.h"
  38. #include "golomb.h"
  39. #include "mathops.h"
  40. #include "mpegutils.h"
  41. #include "rectangle.h"
  42. #include "thread.h"
  43. #include "vdpau_compat.h"
  44. void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
  45. {
  46. int off = offsetof(H264Picture, tf) + sizeof(pic->tf);
  47. int i;
  48. if (!pic->f || !pic->f->buf[0])
  49. return;
  50. ff_thread_release_buffer(h->avctx, &pic->tf);
  51. av_buffer_unref(&pic->hwaccel_priv_buf);
  52. av_buffer_unref(&pic->qscale_table_buf);
  53. av_buffer_unref(&pic->mb_type_buf);
  54. for (i = 0; i < 2; i++) {
  55. av_buffer_unref(&pic->motion_val_buf[i]);
  56. av_buffer_unref(&pic->ref_index_buf[i]);
  57. }
  58. memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
  59. }
  60. int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
  61. {
  62. int ret, i;
  63. av_assert0(!dst->f->buf[0]);
  64. av_assert0(src->f->buf[0]);
  65. src->tf.f = src->f;
  66. dst->tf.f = dst->f;
  67. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  68. if (ret < 0)
  69. goto fail;
  70. dst->qscale_table_buf = av_buffer_ref(src->qscale_table_buf);
  71. dst->mb_type_buf = av_buffer_ref(src->mb_type_buf);
  72. if (!dst->qscale_table_buf || !dst->mb_type_buf)
  73. goto fail;
  74. dst->qscale_table = src->qscale_table;
  75. dst->mb_type = src->mb_type;
  76. for (i = 0; i < 2; i++) {
  77. dst->motion_val_buf[i] = av_buffer_ref(src->motion_val_buf[i]);
  78. dst->ref_index_buf[i] = av_buffer_ref(src->ref_index_buf[i]);
  79. if (!dst->motion_val_buf[i] || !dst->ref_index_buf[i])
  80. goto fail;
  81. dst->motion_val[i] = src->motion_val[i];
  82. dst->ref_index[i] = src->ref_index[i];
  83. }
  84. if (src->hwaccel_picture_private) {
  85. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  86. if (!dst->hwaccel_priv_buf)
  87. goto fail;
  88. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  89. }
  90. for (i = 0; i < 2; i++)
  91. dst->field_poc[i] = src->field_poc[i];
  92. memcpy(dst->ref_poc, src->ref_poc, sizeof(src->ref_poc));
  93. memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
  94. dst->poc = src->poc;
  95. dst->frame_num = src->frame_num;
  96. dst->mmco_reset = src->mmco_reset;
  97. dst->pic_id = src->pic_id;
  98. dst->long_ref = src->long_ref;
  99. dst->mbaff = src->mbaff;
  100. dst->field_picture = src->field_picture;
  101. dst->reference = src->reference;
  102. dst->crop = src->crop;
  103. dst->crop_left = src->crop_left;
  104. dst->crop_top = src->crop_top;
  105. dst->recovered = src->recovered;
  106. dst->invalid_gap = src->invalid_gap;
  107. dst->sei_recovery_frame_cnt = src->sei_recovery_frame_cnt;
  108. return 0;
  109. fail:
  110. ff_h264_unref_picture(h, dst);
  111. return ret;
  112. }
  113. void ff_h264_set_erpic(ERPicture *dst, H264Picture *src)
  114. {
  115. #if CONFIG_ERROR_RESILIENCE
  116. int i;
  117. memset(dst, 0, sizeof(*dst));
  118. if (!src)
  119. return;
  120. dst->f = src->f;
  121. dst->tf = &src->tf;
  122. for (i = 0; i < 2; i++) {
  123. dst->motion_val[i] = src->motion_val[i];
  124. dst->ref_index[i] = src->ref_index[i];
  125. }
  126. dst->mb_type = src->mb_type;
  127. dst->field_picture = src->field_picture;
  128. #endif /* CONFIG_ERROR_RESILIENCE */
  129. }
  130. int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup)
  131. {
  132. AVCodecContext *const avctx = h->avctx;
  133. int err = 0;
  134. h->mb_y = 0;
  135. #if FF_API_CAP_VDPAU
  136. if (CONFIG_H264_VDPAU_DECODER &&
  137. h->avctx->codec->capabilities & AV_CODEC_CAP_HWACCEL_VDPAU)
  138. ff_vdpau_h264_set_reference_frames(h);
  139. #endif
  140. if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  141. if (!h->droppable) {
  142. err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  143. h->poc.prev_poc_msb = h->poc.poc_msb;
  144. h->poc.prev_poc_lsb = h->poc.poc_lsb;
  145. }
  146. h->poc.prev_frame_num_offset = h->poc.frame_num_offset;
  147. h->poc.prev_frame_num = h->poc.frame_num;
  148. }
  149. if (avctx->hwaccel) {
  150. err = avctx->hwaccel->end_frame(avctx);
  151. if (err < 0)
  152. av_log(avctx, AV_LOG_ERROR,
  153. "hardware accelerator failed to decode picture\n");
  154. }
  155. #if FF_API_CAP_VDPAU
  156. if (CONFIG_H264_VDPAU_DECODER &&
  157. h->avctx->codec->capabilities & AV_CODEC_CAP_HWACCEL_VDPAU)
  158. ff_vdpau_h264_picture_complete(h);
  159. #endif
  160. if (!in_setup && !h->droppable)
  161. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  162. h->picture_structure == PICT_BOTTOM_FIELD);
  163. emms_c();
  164. h->current_slice = 0;
  165. return err;
  166. }