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.

322 lines
11KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. if (pwt->luma_log2_weight_denom > 7U) {
  34. av_log(NULL, AV_LOG_ERROR, "luma_log2_weight_denom %d is out of range\n", pwt->luma_log2_weight_denom);
  35. pwt->luma_log2_weight_denom = 0;
  36. }
  37. if (pwt->chroma_log2_weight_denom > 7U) {
  38. av_log(NULL, AV_LOG_ERROR, "chroma_log2_weight_denom %d is out of range\n", pwt->chroma_log2_weight_denom);
  39. pwt->chroma_log2_weight_denom = 0;
  40. }
  41. luma_def = 1 << pwt->luma_log2_weight_denom;
  42. chroma_def = 1 << pwt->chroma_log2_weight_denom;
  43. for (list = 0; list < 2; list++) {
  44. pwt->luma_weight_flag[list] = 0;
  45. pwt->chroma_weight_flag[list] = 0;
  46. for (i = 0; i < ref_count[list]; i++) {
  47. int luma_weight_flag, chroma_weight_flag;
  48. luma_weight_flag = get_bits1(gb);
  49. if (luma_weight_flag) {
  50. pwt->luma_weight[i][list][0] = get_se_golomb(gb);
  51. pwt->luma_weight[i][list][1] = get_se_golomb(gb);
  52. if (pwt->luma_weight[i][list][0] != luma_def ||
  53. pwt->luma_weight[i][list][1] != 0) {
  54. pwt->use_weight = 1;
  55. pwt->luma_weight_flag[list] = 1;
  56. }
  57. } else {
  58. pwt->luma_weight[i][list][0] = luma_def;
  59. pwt->luma_weight[i][list][1] = 0;
  60. }
  61. if (sps->chroma_format_idc) {
  62. chroma_weight_flag = get_bits1(gb);
  63. if (chroma_weight_flag) {
  64. int j;
  65. for (j = 0; j < 2; j++) {
  66. pwt->chroma_weight[i][list][j][0] = get_se_golomb(gb);
  67. pwt->chroma_weight[i][list][j][1] = get_se_golomb(gb);
  68. if (pwt->chroma_weight[i][list][j][0] != chroma_def ||
  69. pwt->chroma_weight[i][list][j][1] != 0) {
  70. pwt->use_weight_chroma = 1;
  71. pwt->chroma_weight_flag[list] = 1;
  72. }
  73. }
  74. } else {
  75. int j;
  76. for (j = 0; j < 2; j++) {
  77. pwt->chroma_weight[i][list][j][0] = chroma_def;
  78. pwt->chroma_weight[i][list][j][1] = 0;
  79. }
  80. }
  81. }
  82. }
  83. if (slice_type_nos != AV_PICTURE_TYPE_B)
  84. break;
  85. }
  86. pwt->use_weight = pwt->use_weight || pwt->use_weight_chroma;
  87. return 0;
  88. }
  89. /**
  90. * Check if the top & left blocks are available if needed and
  91. * change the dc mode so it only uses the available blocks.
  92. */
  93. int ff_h264_check_intra4x4_pred_mode(int8_t *pred_mode_cache, void *logctx,
  94. int top_samples_available, int left_samples_available)
  95. {
  96. static const int8_t top[12] = {
  97. -1, 0, LEFT_DC_PRED, -1, -1, -1, -1, -1, 0
  98. };
  99. static const int8_t left[12] = {
  100. 0, -1, TOP_DC_PRED, 0, -1, -1, -1, 0, -1, DC_128_PRED
  101. };
  102. int i;
  103. if (!(top_samples_available & 0x8000)) {
  104. for (i = 0; i < 4; i++) {
  105. int status = top[pred_mode_cache[scan8[0] + i]];
  106. if (status < 0) {
  107. av_log(logctx, AV_LOG_ERROR,
  108. "top block unavailable for requested intra mode %d\n",
  109. status);
  110. return AVERROR_INVALIDDATA;
  111. } else if (status) {
  112. pred_mode_cache[scan8[0] + i] = status;
  113. }
  114. }
  115. }
  116. if ((left_samples_available & 0x8888) != 0x8888) {
  117. static const int mask[4] = { 0x8000, 0x2000, 0x80, 0x20 };
  118. for (i = 0; i < 4; i++)
  119. if (!(left_samples_available & mask[i])) {
  120. int status = left[pred_mode_cache[scan8[0] + 8 * i]];
  121. if (status < 0) {
  122. av_log(logctx, AV_LOG_ERROR,
  123. "left block unavailable for requested intra4x4 mode %d\n",
  124. status);
  125. return AVERROR_INVALIDDATA;
  126. } else if (status) {
  127. pred_mode_cache[scan8[0] + 8 * i] = status;
  128. }
  129. }
  130. }
  131. return 0;
  132. }
  133. /**
  134. * Check if the top & left blocks are available if needed and
  135. * change the dc mode so it only uses the available blocks.
  136. */
  137. int ff_h264_check_intra_pred_mode(void *logctx, int top_samples_available,
  138. int left_samples_available,
  139. int mode, int is_chroma)
  140. {
  141. static const int8_t top[4] = { LEFT_DC_PRED8x8, 1, -1, -1 };
  142. static const int8_t left[5] = { TOP_DC_PRED8x8, -1, 2, -1, DC_128_PRED8x8 };
  143. if (mode > 3U) {
  144. av_log(logctx, AV_LOG_ERROR,
  145. "out of range intra chroma pred mode\n");
  146. return AVERROR_INVALIDDATA;
  147. }
  148. if (!(top_samples_available & 0x8000)) {
  149. mode = top[mode];
  150. if (mode < 0) {
  151. av_log(logctx, AV_LOG_ERROR,
  152. "top block unavailable for requested intra mode\n");
  153. return AVERROR_INVALIDDATA;
  154. }
  155. }
  156. if ((left_samples_available & 0x8080) != 0x8080) {
  157. mode = left[mode];
  158. if (mode < 0) {
  159. av_log(logctx, AV_LOG_ERROR,
  160. "left block unavailable for requested intra mode\n");
  161. return AVERROR_INVALIDDATA;
  162. }
  163. if (is_chroma && (left_samples_available & 0x8080)) {
  164. // mad cow disease mode, aka MBAFF + constrained_intra_pred
  165. mode = ALZHEIMER_DC_L0T_PRED8x8 +
  166. (!(left_samples_available & 0x8000)) +
  167. 2 * (mode == DC_128_PRED8x8);
  168. }
  169. }
  170. return mode;
  171. }
  172. int ff_h264_parse_ref_count(int *plist_count, int ref_count[2],
  173. GetBitContext *gb, const PPS *pps,
  174. int slice_type_nos, int picture_structure, void *logctx)
  175. {
  176. int list_count;
  177. int num_ref_idx_active_override_flag;
  178. // set defaults, might be overridden a few lines later
  179. ref_count[0] = pps->ref_count[0];
  180. ref_count[1] = pps->ref_count[1];
  181. if (slice_type_nos != AV_PICTURE_TYPE_I) {
  182. unsigned max[2];
  183. max[0] = max[1] = picture_structure == PICT_FRAME ? 15 : 31;
  184. num_ref_idx_active_override_flag = get_bits1(gb);
  185. if (num_ref_idx_active_override_flag) {
  186. ref_count[0] = get_ue_golomb(gb) + 1;
  187. if (slice_type_nos == AV_PICTURE_TYPE_B) {
  188. ref_count[1] = get_ue_golomb(gb) + 1;
  189. } else
  190. // full range is spec-ok in this case, even for frames
  191. ref_count[1] = 1;
  192. }
  193. if (ref_count[0] - 1 > max[0] || ref_count[1] - 1 > max[1]) {
  194. av_log(logctx, AV_LOG_ERROR, "reference overflow %u > %u or %u > %u\n",
  195. ref_count[0] - 1, max[0], ref_count[1] - 1, max[1]);
  196. ref_count[0] = ref_count[1] = 0;
  197. *plist_count = 0;
  198. goto fail;
  199. }
  200. if (slice_type_nos == AV_PICTURE_TYPE_B)
  201. list_count = 2;
  202. else
  203. list_count = 1;
  204. } else {
  205. list_count = 0;
  206. ref_count[0] = ref_count[1] = 0;
  207. }
  208. *plist_count = list_count;
  209. return 0;
  210. fail:
  211. *plist_count = 0;
  212. ref_count[0] = 0;
  213. ref_count[1] = 0;
  214. return AVERROR_INVALIDDATA;
  215. }
  216. int ff_h264_init_poc(int pic_field_poc[2], int *pic_poc,
  217. const SPS *sps, H264POCContext *pc,
  218. int picture_structure, int nal_ref_idc)
  219. {
  220. const int max_frame_num = 1 << sps->log2_max_frame_num;
  221. int field_poc[2];
  222. pc->frame_num_offset = pc->prev_frame_num_offset;
  223. if (pc->frame_num < pc->prev_frame_num)
  224. pc->frame_num_offset += max_frame_num;
  225. if (sps->poc_type == 0) {
  226. const int max_poc_lsb = 1 << sps->log2_max_poc_lsb;
  227. if (pc->poc_lsb < pc->prev_poc_lsb &&
  228. pc->prev_poc_lsb - pc->poc_lsb >= max_poc_lsb / 2)
  229. pc->poc_msb = pc->prev_poc_msb + max_poc_lsb;
  230. else if (pc->poc_lsb > pc->prev_poc_lsb &&
  231. pc->prev_poc_lsb - pc->poc_lsb < -max_poc_lsb / 2)
  232. pc->poc_msb = pc->prev_poc_msb - max_poc_lsb;
  233. else
  234. pc->poc_msb = pc->prev_poc_msb;
  235. field_poc[0] =
  236. field_poc[1] = pc->poc_msb + pc->poc_lsb;
  237. if (picture_structure == PICT_FRAME)
  238. field_poc[1] += pc->delta_poc_bottom;
  239. } else if (sps->poc_type == 1) {
  240. int abs_frame_num, expected_delta_per_poc_cycle, expectedpoc;
  241. int i;
  242. if (sps->poc_cycle_length != 0)
  243. abs_frame_num = pc->frame_num_offset + pc->frame_num;
  244. else
  245. abs_frame_num = 0;
  246. if (nal_ref_idc == 0 && abs_frame_num > 0)
  247. abs_frame_num--;
  248. expected_delta_per_poc_cycle = 0;
  249. for (i = 0; i < sps->poc_cycle_length; i++)
  250. // FIXME integrate during sps parse
  251. expected_delta_per_poc_cycle += sps->offset_for_ref_frame[i];
  252. if (abs_frame_num > 0) {
  253. int poc_cycle_cnt = (abs_frame_num - 1) / sps->poc_cycle_length;
  254. int frame_num_in_poc_cycle = (abs_frame_num - 1) % sps->poc_cycle_length;
  255. expectedpoc = poc_cycle_cnt * expected_delta_per_poc_cycle;
  256. for (i = 0; i <= frame_num_in_poc_cycle; i++)
  257. expectedpoc = expectedpoc + sps->offset_for_ref_frame[i];
  258. } else
  259. expectedpoc = 0;
  260. if (nal_ref_idc == 0)
  261. expectedpoc = expectedpoc + sps->offset_for_non_ref_pic;
  262. field_poc[0] = expectedpoc + pc->delta_poc[0];
  263. field_poc[1] = field_poc[0] + sps->offset_for_top_to_bottom_field;
  264. if (picture_structure == PICT_FRAME)
  265. field_poc[1] += pc->delta_poc[1];
  266. } else {
  267. int poc = 2 * (pc->frame_num_offset + pc->frame_num);
  268. if (!nal_ref_idc)
  269. poc--;
  270. field_poc[0] = poc;
  271. field_poc[1] = poc;
  272. }
  273. if (picture_structure != PICT_BOTTOM_FIELD)
  274. pic_field_poc[0] = field_poc[0];
  275. if (picture_structure != PICT_TOP_FIELD)
  276. pic_field_poc[1] = field_poc[1];
  277. *pic_poc = FFMIN(pic_field_poc[0], pic_field_poc[1]);
  278. return 0;
  279. }