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.

228 lines
7.8KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "get_bits.h"
  19. #include "golomb.h"
  20. #include "h264.h"
  21. #include "h264_parse.h"
  22. int ff_h264_pred_weight_table(GetBitContext *gb, const SPS *sps,
  23. const int *ref_count, int slice_type_nos,
  24. H264PredWeightTable *pwt)
  25. {
  26. int list, i;
  27. int luma_def, chroma_def;
  28. pwt->use_weight = 0;
  29. pwt->use_weight_chroma = 0;
  30. pwt->luma_log2_weight_denom = get_ue_golomb(gb);
  31. if (sps->chroma_format_idc)
  32. pwt->chroma_log2_weight_denom = get_ue_golomb(gb);
  33. luma_def = 1 << pwt->luma_log2_weight_denom;
  34. chroma_def = 1 << pwt->chroma_log2_weight_denom;
  35. for (list = 0; list < 2; list++) {
  36. pwt->luma_weight_flag[list] = 0;
  37. pwt->chroma_weight_flag[list] = 0;
  38. for (i = 0; i < ref_count[list]; i++) {
  39. int luma_weight_flag, chroma_weight_flag;
  40. luma_weight_flag = get_bits1(gb);
  41. if (luma_weight_flag) {
  42. pwt->luma_weight[i][list][0] = get_se_golomb(gb);
  43. pwt->luma_weight[i][list][1] = get_se_golomb(gb);
  44. if (pwt->luma_weight[i][list][0] != luma_def ||
  45. pwt->luma_weight[i][list][1] != 0) {
  46. pwt->use_weight = 1;
  47. pwt->luma_weight_flag[list] = 1;
  48. }
  49. } else {
  50. pwt->luma_weight[i][list][0] = luma_def;
  51. pwt->luma_weight[i][list][1] = 0;
  52. }
  53. if (sps->chroma_format_idc) {
  54. chroma_weight_flag = get_bits1(gb);
  55. if (chroma_weight_flag) {
  56. int j;
  57. for (j = 0; j < 2; j++) {
  58. pwt->chroma_weight[i][list][j][0] = get_se_golomb(gb);
  59. pwt->chroma_weight[i][list][j][1] = get_se_golomb(gb);
  60. if (pwt->chroma_weight[i][list][j][0] != chroma_def ||
  61. pwt->chroma_weight[i][list][j][1] != 0) {
  62. pwt->use_weight_chroma = 1;
  63. pwt->chroma_weight_flag[list] = 1;
  64. }
  65. }
  66. } else {
  67. int j;
  68. for (j = 0; j < 2; j++) {
  69. pwt->chroma_weight[i][list][j][0] = chroma_def;
  70. pwt->chroma_weight[i][list][j][1] = 0;
  71. }
  72. }
  73. }
  74. }
  75. if (slice_type_nos != AV_PICTURE_TYPE_B)
  76. break;
  77. }
  78. pwt->use_weight = pwt->use_weight || pwt->use_weight_chroma;
  79. return 0;
  80. }
  81. /**
  82. * Check if the top & left blocks are available if needed and
  83. * change the dc mode so it only uses the available blocks.
  84. */
  85. int ff_h264_check_intra4x4_pred_mode(int8_t *pred_mode_cache, void *logctx,
  86. int top_samples_available, int left_samples_available)
  87. {
  88. static const int8_t top[12] = {
  89. -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
  90. };
  91. static const int8_t left[12] = {
  92. 0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
  93. };
  94. int i;
  95. if (!(top_samples_available & 0x8000)) {
  96. for (i = 0; i < 4; i++) {
  97. int status = top[pred_mode_cache[scan8[0] + i]];
  98. if (status < 0) {
  99. av_log(logctx, AV_LOG_ERROR,
  100. "top block unavailable for requested intra4x4 mode %d\n",
  101. status);
  102. return AVERROR_INVALIDDATA;
  103. } else if (status) {
  104. pred_mode_cache[scan8[0] + i] = status;
  105. }
  106. }
  107. }
  108. if ((left_samples_available & 0x8888) != 0x8888) {
  109. static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
  110. for (i = 0; i < 4; i++)
  111. if (!(left_samples_available & mask[i])) {
  112. int status = left[pred_mode_cache[scan8[0] + 8 * i]];
  113. if (status < 0) {
  114. av_log(logctx, AV_LOG_ERROR,
  115. "left block unavailable for requested intra4x4 mode %d\n",
  116. status);
  117. return AVERROR_INVALIDDATA;
  118. } else if (status) {
  119. pred_mode_cache[scan8[0] + 8 * i] = status;
  120. }
  121. }
  122. }
  123. return 0;
  124. }
  125. /**
  126. * Check if the top & left blocks are available if needed and
  127. * change the dc mode so it only uses the available blocks.
  128. */
  129. int ff_h264_check_intra_pred_mode(void *logctx, int top_samples_available,
  130. int left_samples_available,
  131. int mode, int is_chroma)
  132. {
  133. static const int8_t top[4] = { LEFT_DC_PRED8x8, 1, -1, -1 };
  134. static const int8_t left[5] = { TOP_DC_PRED8x8, -1, 2, -1, DC_128_PRED8x8 };
  135. if (mode > 3U) {
  136. av_log(logctx, AV_LOG_ERROR,
  137. "out of range intra chroma pred mode\n");
  138. return AVERROR_INVALIDDATA;
  139. }
  140. if (!(top_samples_available & 0x8000)) {
  141. mode = top[mode];
  142. if (mode < 0) {
  143. av_log(logctx, AV_LOG_ERROR,
  144. "top block unavailable for requested intra mode\n");
  145. return AVERROR_INVALIDDATA;
  146. }
  147. }
  148. if ((left_samples_available & 0x8080) != 0x8080) {
  149. mode = left[mode];
  150. if (is_chroma && (left_samples_available & 0x8080)) {
  151. // mad cow disease mode, aka MBAFF + constrained_intra_pred
  152. mode = ALZHEIMER_DC_L0T_PRED8x8 +
  153. (!(left_samples_available & 0x8000)) +
  154. 2 * (mode == DC_128_PRED8x8);
  155. }
  156. if (mode < 0) {
  157. av_log(logctx, AV_LOG_ERROR,
  158. "left block unavailable for requested intra mode\n");
  159. return AVERROR_INVALIDDATA;
  160. }
  161. }
  162. return mode;
  163. }
  164. int ff_h264_parse_ref_count(int *plist_count, int ref_count[2],
  165. GetBitContext *gb, const PPS *pps,
  166. int slice_type_nos, int picture_structure)
  167. {
  168. int list_count;
  169. int num_ref_idx_active_override_flag, max_refs;
  170. // set defaults, might be overridden a few lines later
  171. ref_count[0] = pps->ref_count[0];
  172. ref_count[1] = pps->ref_count[1];
  173. if (slice_type_nos != AV_PICTURE_TYPE_I) {
  174. num_ref_idx_active_override_flag = get_bits1(gb);
  175. if (num_ref_idx_active_override_flag) {
  176. ref_count[0] = get_ue_golomb(gb) + 1;
  177. if (ref_count[0] < 1)
  178. goto fail;
  179. if (slice_type_nos == AV_PICTURE_TYPE_B) {
  180. ref_count[1] = get_ue_golomb(gb) + 1;
  181. if (ref_count[1] < 1)
  182. goto fail;
  183. }
  184. }
  185. if (slice_type_nos == AV_PICTURE_TYPE_B)
  186. list_count = 2;
  187. else
  188. list_count = 1;
  189. } else {
  190. list_count = 0;
  191. ref_count[0] = ref_count[1] = 0;
  192. }
  193. max_refs = picture_structure == PICT_FRAME ? 16 : 32;
  194. if (ref_count[0] > max_refs || ref_count[1] > max_refs)
  195. goto fail;
  196. *plist_count = list_count;
  197. return 0;
  198. fail:
  199. *plist_count = 0;
  200. ref_count[0] = 0;
  201. ref_count[1] = 0;
  202. return AVERROR_INVALIDDATA;
  203. }