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.

391 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. static int FUNC(frame_sync_code)(CodedBitstreamContext *ctx, RWContext *rw,
  19. VP9RawFrameHeader *current)
  20. {
  21. uint8_t frame_sync_byte_0 = VP9_FRAME_SYNC_0;
  22. uint8_t frame_sync_byte_1 = VP9_FRAME_SYNC_1;
  23. uint8_t frame_sync_byte_2 = VP9_FRAME_SYNC_2;
  24. int err;
  25. xf(8, frame_sync_byte_0, frame_sync_byte_0, 0);
  26. xf(8, frame_sync_byte_1, frame_sync_byte_1, 0);
  27. xf(8, frame_sync_byte_2, frame_sync_byte_2, 0);
  28. if (frame_sync_byte_0 != VP9_FRAME_SYNC_0 ||
  29. frame_sync_byte_1 != VP9_FRAME_SYNC_1 ||
  30. frame_sync_byte_2 != VP9_FRAME_SYNC_2) {
  31. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid frame sync code: "
  32. "%02x %02x %02x.\n", frame_sync_byte_0,
  33. frame_sync_byte_1, frame_sync_byte_2);
  34. return AVERROR_INVALIDDATA;
  35. }
  36. return 0;
  37. }
  38. static int FUNC(color_config)(CodedBitstreamContext *ctx, RWContext *rw,
  39. VP9RawFrameHeader *current, int profile)
  40. {
  41. int err;
  42. if (profile >= 2)
  43. f(1, ten_or_twelve_bit);
  44. f(3, color_space);
  45. if (current->color_space != VP9_CS_RGB) {
  46. f(1, color_range);
  47. if (profile == 1 || profile == 3) {
  48. f(1, subsampling_x);
  49. f(1, subsampling_y);
  50. f(1, color_config_reserved_zero);
  51. } else {
  52. infer(subsampling_x, 1);
  53. infer(subsampling_y, 1);
  54. }
  55. } else {
  56. infer(color_range, 1);
  57. if (profile == 1 || profile == 3) {
  58. infer(subsampling_x, 0);
  59. infer(subsampling_y, 0);
  60. }
  61. }
  62. return 0;
  63. }
  64. static int FUNC(frame_size)(CodedBitstreamContext *ctx, RWContext *rw,
  65. VP9RawFrameHeader *current)
  66. {
  67. CodedBitstreamVP9Context *vp9 = ctx->priv_data;
  68. int err;
  69. f(16, frame_width_minus_1);
  70. f(16, frame_height_minus_1);
  71. vp9->mi_cols = (current->frame_width_minus_1 + 8) >> 3;
  72. vp9->mi_rows = (current->frame_height_minus_1 + 8) >> 3;
  73. vp9->sb64_cols = (vp9->mi_cols + 7) >> 3;
  74. vp9->sb64_rows = (vp9->mi_rows + 7) >> 3;
  75. return 0;
  76. }
  77. static int FUNC(render_size)(CodedBitstreamContext *ctx, RWContext *rw,
  78. VP9RawFrameHeader *current)
  79. {
  80. int err;
  81. f(1, render_and_frame_size_different);
  82. if (current->render_and_frame_size_different) {
  83. f(16, render_width_minus_1);
  84. f(16, render_height_minus_1);
  85. }
  86. return 0;
  87. }
  88. static int FUNC(frame_size_with_refs)(CodedBitstreamContext *ctx, RWContext *rw,
  89. VP9RawFrameHeader *current)
  90. {
  91. int err, i;
  92. for (i = 0; i < VP9_REFS_PER_FRAME; i++) {
  93. fs(1, found_ref[i], 1, i);
  94. if (current->found_ref[i])
  95. break;
  96. }
  97. if (i >= VP9_REFS_PER_FRAME)
  98. CHECK(FUNC(frame_size)(ctx, rw, current));
  99. CHECK(FUNC(render_size)(ctx, rw, current));
  100. return 0;
  101. }
  102. static int FUNC(interpolation_filter)(CodedBitstreamContext *ctx, RWContext *rw,
  103. VP9RawFrameHeader *current)
  104. {
  105. int err;
  106. f(1, is_filter_switchable);
  107. if (!current->is_filter_switchable)
  108. f(2, raw_interpolation_filter_type);
  109. return 0;
  110. }
  111. static int FUNC(loop_filter_params)(CodedBitstreamContext *ctx, RWContext *rw,
  112. VP9RawFrameHeader *current)
  113. {
  114. int err, i;
  115. f(6, loop_filter_level);
  116. f(3, loop_filter_sharpness);
  117. f(1, loop_filter_delta_enabled);
  118. if (current->loop_filter_delta_enabled) {
  119. f(1, loop_filter_delta_update);
  120. if (current->loop_filter_delta_update) {
  121. for (i = 0; i < VP9_MAX_REF_FRAMES; i++) {
  122. fs(1, update_ref_delta[i], 1, i);
  123. if (current->update_ref_delta[i])
  124. ss(6, loop_filter_ref_deltas[i], 1, i);
  125. }
  126. for (i = 0; i < 2; i++) {
  127. fs(1, update_mode_delta[i], 1, i);
  128. if (current->update_mode_delta[i])
  129. ss(6, loop_filter_mode_deltas[i], 1, i);
  130. }
  131. }
  132. }
  133. return 0;
  134. }
  135. static int FUNC(quantization_params)(CodedBitstreamContext *ctx, RWContext *rw,
  136. VP9RawFrameHeader *current)
  137. {
  138. int err;
  139. f(8, base_q_idx);
  140. delta_q(delta_q_y_dc);
  141. delta_q(delta_q_uv_dc);
  142. delta_q(delta_q_uv_ac);
  143. return 0;
  144. }
  145. static int FUNC(segmentation_params)(CodedBitstreamContext *ctx, RWContext *rw,
  146. VP9RawFrameHeader *current)
  147. {
  148. static const int segmentation_feature_bits[VP9_SEG_LVL_MAX] = { 8, 6, 2, 0 };
  149. static const int segmentation_feature_signed[VP9_SEG_LVL_MAX] = { 1, 1, 0, 0 };
  150. int err, i, j;
  151. f(1, segmentation_enabled);
  152. if (current->segmentation_enabled) {
  153. f(1, segmentation_update_map);
  154. if (current->segmentation_update_map) {
  155. for (i = 0; i < 7; i++)
  156. prob(segmentation_tree_probs[i], 1, i);
  157. f(1, segmentation_temporal_update);
  158. for (i = 0; i < 3; i++) {
  159. if (current->segmentation_temporal_update)
  160. prob(segmentation_pred_prob[i], 1, i);
  161. else
  162. infer(segmentation_pred_prob[i], 255);
  163. }
  164. }
  165. f(1, segmentation_update_data);
  166. if (current->segmentation_update_data) {
  167. f(1, segmentation_abs_or_delta_update);
  168. for (i = 0; i < VP9_MAX_SEGMENTS; i++) {
  169. for (j = 0; j < VP9_SEG_LVL_MAX; j++) {
  170. fs(1, feature_enabled[i][j], 2, i, j);
  171. if (current->feature_enabled[i][j] &&
  172. segmentation_feature_bits[j]) {
  173. fs(segmentation_feature_bits[j],
  174. feature_value[i][j], 2, i, j);
  175. if (segmentation_feature_signed[j])
  176. fs(1, feature_sign[i][j], 2, i, j);
  177. else
  178. infer(feature_sign[i][j], 0);
  179. } else {
  180. infer(feature_value[i][j], 0);
  181. infer(feature_sign[i][j], 0);
  182. }
  183. }
  184. }
  185. }
  186. }
  187. return 0;
  188. }
  189. static int FUNC(tile_info)(CodedBitstreamContext *ctx, RWContext *rw,
  190. VP9RawFrameHeader *current)
  191. {
  192. CodedBitstreamVP9Context *vp9 = ctx->priv_data;
  193. int min_log2_tile_cols, max_log2_tile_cols;
  194. int err;
  195. min_log2_tile_cols = 0;
  196. while ((VP9_MAX_TILE_WIDTH_B64 << min_log2_tile_cols) < vp9->sb64_cols)
  197. ++min_log2_tile_cols;
  198. max_log2_tile_cols = 0;
  199. while ((vp9->sb64_cols >> (max_log2_tile_cols + 1)) >= VP9_MIN_TILE_WIDTH_B64)
  200. ++max_log2_tile_cols;
  201. increment(tile_cols_log2, min_log2_tile_cols, max_log2_tile_cols);
  202. increment(tile_rows_log2, 0, 2);
  203. return 0;
  204. }
  205. static int FUNC(uncompressed_header)(CodedBitstreamContext *ctx, RWContext *rw,
  206. VP9RawFrameHeader *current)
  207. {
  208. int profile, i;
  209. int err;
  210. f(2, frame_marker);
  211. f(1, profile_low_bit);
  212. f(1, profile_high_bit);
  213. profile = (current->profile_high_bit << 1) + current->profile_low_bit;
  214. if (profile == 3)
  215. f(1, profile_reserved_zero);
  216. f(1, show_existing_frame);
  217. if (current->show_existing_frame) {
  218. f(3, frame_to_show_map_idx);
  219. infer(header_size_in_bytes, 0);
  220. infer(refresh_frame_flags, 0x00);
  221. infer(loop_filter_level, 0);
  222. return 0;
  223. }
  224. f(1, frame_type);
  225. f(1, show_frame);
  226. f(1, error_resilient_mode);
  227. if (current->frame_type == VP9_KEY_FRAME) {
  228. CHECK(FUNC(frame_sync_code)(ctx, rw, current));
  229. CHECK(FUNC(color_config)(ctx, rw, current, profile));
  230. CHECK(FUNC(frame_size)(ctx, rw, current));
  231. CHECK(FUNC(render_size)(ctx, rw, current));
  232. infer(refresh_frame_flags, 0xff);
  233. } else {
  234. if (current->show_frame == 0)
  235. f(1, intra_only);
  236. else
  237. infer(intra_only, 0);
  238. if (current->error_resilient_mode == 0)
  239. f(2, reset_frame_context);
  240. else
  241. infer(reset_frame_context, 0);
  242. if (current->intra_only == 1) {
  243. CHECK(FUNC(frame_sync_code)(ctx, rw, current));
  244. if (profile > 0) {
  245. CHECK(FUNC(color_config)(ctx, rw, current, profile));
  246. } else {
  247. infer(color_space, 1);
  248. infer(subsampling_x, 1);
  249. infer(subsampling_y, 1);
  250. }
  251. f(8, refresh_frame_flags);
  252. CHECK(FUNC(frame_size)(ctx, rw, current));
  253. CHECK(FUNC(render_size)(ctx, rw, current));
  254. } else {
  255. f(8, refresh_frame_flags);
  256. for (i = 0; i < VP9_REFS_PER_FRAME; i++) {
  257. fs(3, ref_frame_idx[i], 1, i);
  258. fs(1, ref_frame_sign_bias[VP9_LAST_FRAME + i],
  259. 1, VP9_LAST_FRAME + i);
  260. }
  261. CHECK(FUNC(frame_size_with_refs)(ctx, rw, current));
  262. f(1, allow_high_precision_mv);
  263. CHECK(FUNC(interpolation_filter)(ctx, rw, current));
  264. }
  265. }
  266. if (current->error_resilient_mode == 0) {
  267. f(1, refresh_frame_context);
  268. f(1, frame_parallel_decoding_mode);
  269. } else {
  270. infer(refresh_frame_context, 0);
  271. infer(frame_parallel_decoding_mode, 1);
  272. }
  273. f(2, frame_context_idx);
  274. CHECK(FUNC(loop_filter_params)(ctx, rw, current));
  275. CHECK(FUNC(quantization_params)(ctx, rw, current));
  276. CHECK(FUNC(segmentation_params)(ctx, rw, current));
  277. CHECK(FUNC(tile_info)(ctx, rw, current));
  278. f(16, header_size_in_bytes);
  279. return 0;
  280. }
  281. static int FUNC(trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw)
  282. {
  283. int err;
  284. av_unused int zero = 0;
  285. while (byte_alignment(rw) != 0)
  286. xf(1, zero_bit, zero, 0);
  287. return 0;
  288. }
  289. static int FUNC(frame)(CodedBitstreamContext *ctx, RWContext *rw,
  290. VP9RawFrame *current)
  291. {
  292. int err;
  293. HEADER("Frame");
  294. CHECK(FUNC(uncompressed_header)(ctx, rw, &current->header));
  295. CHECK(FUNC(trailing_bits)(ctx, rw));
  296. return 0;
  297. }
  298. static int FUNC(superframe_index)(CodedBitstreamContext *ctx, RWContext *rw,
  299. VP9RawSuperframeIndex *current)
  300. {
  301. int err, i;
  302. HEADER("Superframe Index");
  303. f(3, superframe_marker);
  304. f(2, bytes_per_framesize_minus_1);
  305. f(3, frames_in_superframe_minus_1);
  306. for (i = 0; i <= current->frames_in_superframe_minus_1; i++) {
  307. // Surprise little-endian!
  308. fle(8 * (current->bytes_per_framesize_minus_1 + 1),
  309. frame_sizes[i], 1, i);
  310. }
  311. f(3, superframe_marker);
  312. f(2, bytes_per_framesize_minus_1);
  313. f(3, frames_in_superframe_minus_1);
  314. return 0;
  315. }