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.

200 lines
6.1KB

  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/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 "h264dec.h"
  35. #include "h264data.h"
  36. #include "h264chroma.h"
  37. #include "h264_mvpred.h"
  38. #include "mathops.h"
  39. #include "mpegutils.h"
  40. #include "rectangle.h"
  41. #include "thread.h"
  42. void ff_h264_unref_picture(H264Context *h, H264Picture *pic)
  43. {
  44. int off = offsetof(H264Picture, tf) + sizeof(pic->tf);
  45. int i;
  46. if (!pic->f || !pic->f->buf[0])
  47. return;
  48. ff_thread_release_buffer(h->avctx, &pic->tf);
  49. av_buffer_unref(&pic->hwaccel_priv_buf);
  50. av_buffer_unref(&pic->qscale_table_buf);
  51. av_buffer_unref(&pic->mb_type_buf);
  52. for (i = 0; i < 2; i++) {
  53. av_buffer_unref(&pic->motion_val_buf[i]);
  54. av_buffer_unref(&pic->ref_index_buf[i]);
  55. }
  56. memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
  57. }
  58. int ff_h264_ref_picture(H264Context *h, H264Picture *dst, H264Picture *src)
  59. {
  60. int ret, i;
  61. av_assert0(!dst->f->buf[0]);
  62. av_assert0(src->f->buf[0]);
  63. src->tf.f = src->f;
  64. dst->tf.f = dst->f;
  65. ret = ff_thread_ref_frame(&dst->tf, &src->tf);
  66. if (ret < 0)
  67. goto fail;
  68. dst->qscale_table_buf = av_buffer_ref(src->qscale_table_buf);
  69. dst->mb_type_buf = av_buffer_ref(src->mb_type_buf);
  70. if (!dst->qscale_table_buf || !dst->mb_type_buf)
  71. goto fail;
  72. dst->qscale_table = src->qscale_table;
  73. dst->mb_type = src->mb_type;
  74. for (i = 0; i < 2; i++) {
  75. dst->motion_val_buf[i] = av_buffer_ref(src->motion_val_buf[i]);
  76. dst->ref_index_buf[i] = av_buffer_ref(src->ref_index_buf[i]);
  77. if (!dst->motion_val_buf[i] || !dst->ref_index_buf[i])
  78. goto fail;
  79. dst->motion_val[i] = src->motion_val[i];
  80. dst->ref_index[i] = src->ref_index[i];
  81. }
  82. if (src->hwaccel_picture_private) {
  83. dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  84. if (!dst->hwaccel_priv_buf)
  85. goto fail;
  86. dst->hwaccel_picture_private = dst->hwaccel_priv_buf->data;
  87. }
  88. for (i = 0; i < 2; i++)
  89. dst->field_poc[i] = src->field_poc[i];
  90. memcpy(dst->ref_poc, src->ref_poc, sizeof(src->ref_poc));
  91. memcpy(dst->ref_count, src->ref_count, sizeof(src->ref_count));
  92. dst->poc = src->poc;
  93. dst->frame_num = src->frame_num;
  94. dst->mmco_reset = src->mmco_reset;
  95. dst->pic_id = src->pic_id;
  96. dst->long_ref = src->long_ref;
  97. dst->mbaff = src->mbaff;
  98. dst->field_picture = src->field_picture;
  99. dst->reference = src->reference;
  100. dst->recovered = src->recovered;
  101. return 0;
  102. fail:
  103. ff_h264_unref_picture(h, dst);
  104. return ret;
  105. }
  106. #if CONFIG_ERROR_RESILIENCE
  107. static void h264_set_erpic(ERPicture *dst, H264Picture *src)
  108. {
  109. int i;
  110. if (!src)
  111. return;
  112. dst->f = src->f;
  113. dst->tf = &src->tf;
  114. for (i = 0; i < 2; i++) {
  115. dst->motion_val[i] = src->motion_val[i];
  116. dst->ref_index[i] = src->ref_index[i];
  117. }
  118. dst->mb_type = src->mb_type;
  119. dst->field_picture = src->field_picture;
  120. }
  121. #endif /* CONFIG_ERROR_RESILIENCE */
  122. int ff_h264_field_end(H264Context *h, H264SliceContext *sl, int in_setup)
  123. {
  124. AVCodecContext *const avctx = h->avctx;
  125. int err = 0;
  126. h->mb_y = 0;
  127. if (!in_setup && !h->droppable)
  128. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  129. h->picture_structure == PICT_BOTTOM_FIELD);
  130. if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  131. if (!h->droppable) {
  132. err = ff_h264_execute_ref_pic_marking(h);
  133. h->poc.prev_poc_msb = h->poc.poc_msb;
  134. h->poc.prev_poc_lsb = h->poc.poc_lsb;
  135. }
  136. h->poc.prev_frame_num_offset = h->poc.frame_num_offset;
  137. h->poc.prev_frame_num = h->poc.frame_num;
  138. }
  139. if (avctx->hwaccel) {
  140. if (avctx->hwaccel->end_frame(avctx) < 0)
  141. av_log(avctx, AV_LOG_ERROR,
  142. "hardware accelerator failed to decode picture\n");
  143. }
  144. #if CONFIG_ERROR_RESILIENCE
  145. /*
  146. * FIXME: Error handling code does not seem to support interlaced
  147. * when slices span multiple rows
  148. * The ff_er_add_slice calls don't work right for bottom
  149. * fields; they cause massive erroneous error concealing
  150. * Error marking covers both fields (top and bottom).
  151. * This causes a mismatched s->error_count
  152. * and a bad error table. Further, the error count goes to
  153. * INT_MAX when called for bottom field, because mb_y is
  154. * past end by one (callers fault) and resync_mb_y != 0
  155. * causes problems for the first MB line, too.
  156. */
  157. if (!FIELD_PICTURE(h) && h->enable_er) {
  158. h264_set_erpic(&sl->er.cur_pic, h->cur_pic_ptr);
  159. h264_set_erpic(&sl->er.last_pic,
  160. sl->ref_count[0] ? sl->ref_list[0][0].parent : NULL);
  161. h264_set_erpic(&sl->er.next_pic,
  162. sl->ref_count[1] ? sl->ref_list[1][0].parent : NULL);
  163. ff_er_frame_end(&sl->er);
  164. }
  165. #endif /* CONFIG_ERROR_RESILIENCE */
  166. emms_c();
  167. h->current_slice = 0;
  168. return err;
  169. }