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.

201 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 / MPEG4 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 "dsputil.h"
  33. #include "error_resilience.h"
  34. #include "avcodec.h"
  35. #include "h264.h"
  36. #include "h264data.h"
  37. #include "h264chroma.h"
  38. #include "h264_mvpred.h"
  39. #include "golomb.h"
  40. #include "mathops.h"
  41. #include "mpegutils.h"
  42. #include "rectangle.h"
  43. #include "thread.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.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->needs_realloc = src->needs_realloc;
  102. dst->reference = src->reference;
  103. dst->recovered = src->recovered;
  104. return 0;
  105. fail:
  106. ff_h264_unref_picture(h, dst);
  107. return ret;
  108. }
  109. #if CONFIG_ERROR_RESILIENCE
  110. static void h264_set_erpic(ERPicture *dst, H264Picture *src)
  111. {
  112. int i;
  113. if (!src)
  114. return;
  115. dst->f = &src->f;
  116. dst->tf = &src->tf;
  117. for (i = 0; i < 2; i++) {
  118. dst->motion_val[i] = src->motion_val[i];
  119. dst->ref_index[i] = src->ref_index[i];
  120. }
  121. dst->mb_type = src->mb_type;
  122. dst->field_picture = src->field_picture;
  123. }
  124. #endif /* CONFIG_ERROR_RESILIENCE */
  125. int ff_h264_field_end(H264Context *h, int in_setup)
  126. {
  127. AVCodecContext *const avctx = h->avctx;
  128. int err = 0;
  129. h->mb_y = 0;
  130. if (!in_setup && !h->droppable)
  131. ff_thread_report_progress(&h->cur_pic_ptr->tf, INT_MAX,
  132. h->picture_structure == PICT_BOTTOM_FIELD);
  133. if (in_setup || !(avctx->active_thread_type & FF_THREAD_FRAME)) {
  134. if (!h->droppable) {
  135. err = ff_h264_execute_ref_pic_marking(h, h->mmco, h->mmco_index);
  136. h->prev_poc_msb = h->poc_msb;
  137. h->prev_poc_lsb = h->poc_lsb;
  138. }
  139. h->prev_frame_num_offset = h->frame_num_offset;
  140. h->prev_frame_num = h->frame_num;
  141. h->outputed_poc = h->next_outputed_poc;
  142. }
  143. if (avctx->hwaccel) {
  144. if (avctx->hwaccel->end_frame(avctx) < 0)
  145. av_log(avctx, AV_LOG_ERROR,
  146. "hardware accelerator failed to decode picture\n");
  147. }
  148. /*
  149. * FIXME: Error handling code does not seem to support interlaced
  150. * when slices span multiple rows
  151. * The ff_er_add_slice calls don't work right for bottom
  152. * fields; they cause massive erroneous error concealing
  153. * Error marking covers both fields (top and bottom).
  154. * This causes a mismatched s->error_count
  155. * and a bad error table. Further, the error count goes to
  156. * INT_MAX when called for bottom field, because mb_y is
  157. * past end by one (callers fault) and resync_mb_y != 0
  158. * causes problems for the first MB line, too.
  159. */
  160. if (CONFIG_ERROR_RESILIENCE && !FIELD_PICTURE(h)) {
  161. h264_set_erpic(&h->er.cur_pic, h->cur_pic_ptr);
  162. h264_set_erpic(&h->er.last_pic,
  163. h->ref_count[0] ? &h->ref_list[0][0] : NULL);
  164. h264_set_erpic(&h->er.next_pic,
  165. h->ref_count[1] ? &h->ref_list[1][0] : NULL);
  166. ff_er_frame_end(&h->er);
  167. }
  168. emms_c();
  169. h->current_slice = 0;
  170. return err;
  171. }