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.

308 lines
12KB

  1. /*
  2. * FFV1 codec for libavcodec
  3. *
  4. * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * FF Video Codec 1 (a lossless codec)
  25. */
  26. #include "libavutil/attributes.h"
  27. #include "libavutil/avassert.h"
  28. #include "avcodec.h"
  29. #include "get_bits.h"
  30. #include "put_bits.h"
  31. #include "rangecoder.h"
  32. #include "golomb.h"
  33. #include "mathops.h"
  34. #include "ffv1.h"
  35. const int8_t ffv1_quant5_10bit[256] = {
  36. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
  37. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  38. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  39. 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  40. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  41. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  42. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  43. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  44. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  45. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  46. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  47. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  48. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1,
  49. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  50. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  51. -1, -1, -1, -1, -1, -1, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0,
  52. };
  53. const int8_t ffv1_quant5[256] = {
  54. 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  55. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  56. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  57. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  58. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  59. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  60. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  61. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  62. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  63. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  64. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  65. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  66. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  67. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  68. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  69. -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -1,
  70. };
  71. const int8_t ffv1_quant9_10bit[256] = {
  72. 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
  73. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
  74. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  75. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  76. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  77. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  78. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  79. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  80. -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
  81. -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
  82. -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
  83. -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
  84. -4, -4, -4, -4, -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3,
  85. -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
  86. -3, -3, -3, -3, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  87. -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, -1, -1, -0, -0, -0, -0,
  88. };
  89. const int8_t ffv1_quant11[256] = {
  90. 0, 1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
  91. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  92. 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  93. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  94. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  95. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  96. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  97. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  98. -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
  99. -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
  100. -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
  101. -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
  102. -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5,
  103. -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -4, -4,
  104. -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
  105. -4, -4, -4, -4, -4, -3, -3, -3, -3, -3, -3, -3, -2, -2, -2, -1,
  106. };
  107. const uint8_t ffv1_ver2_state[256] = {
  108. 0, 10, 10, 10, 10, 16, 16, 16, 28, 16, 16, 29, 42, 49, 20, 49,
  109. 59, 25, 26, 26, 27, 31, 33, 33, 33, 34, 34, 37, 67, 38, 39, 39,
  110. 40, 40, 41, 79, 43, 44, 45, 45, 48, 48, 64, 50, 51, 52, 88, 52,
  111. 53, 74, 55, 57, 58, 58, 74, 60, 101, 61, 62, 84, 66, 66, 68, 69,
  112. 87, 82, 71, 97, 73, 73, 82, 75, 111, 77, 94, 78, 87, 81, 83, 97,
  113. 85, 83, 94, 86, 99, 89, 90, 99, 111, 92, 93, 134, 95, 98, 105, 98,
  114. 105, 110, 102, 108, 102, 118, 103, 106, 106, 113, 109, 112, 114, 112, 116, 125,
  115. 115, 116, 117, 117, 126, 119, 125, 121, 121, 123, 145, 124, 126, 131, 127, 129,
  116. 165, 130, 132, 138, 133, 135, 145, 136, 137, 139, 146, 141, 143, 142, 144, 148,
  117. 147, 155, 151, 149, 151, 150, 152, 157, 153, 154, 156, 168, 158, 162, 161, 160,
  118. 172, 163, 169, 164, 166, 184, 167, 170, 177, 174, 171, 173, 182, 176, 180, 178,
  119. 175, 189, 179, 181, 186, 183, 192, 185, 200, 187, 191, 188, 190, 197, 193, 196,
  120. 197, 194, 195, 196, 198, 202, 199, 201, 210, 203, 207, 204, 205, 206, 208, 214,
  121. 209, 211, 221, 212, 213, 215, 224, 216, 217, 218, 219, 220, 222, 228, 223, 225,
  122. 226, 224, 227, 229, 240, 230, 231, 232, 233, 234, 235, 236, 238, 239, 237, 242,
  123. 241, 243, 242, 244, 245, 246, 247, 248, 249, 250, 251, 252, 252, 253, 254, 255,
  124. };
  125. av_cold int ffv1_common_init(AVCodecContext *avctx)
  126. {
  127. FFV1Context *s = avctx->priv_data;
  128. s->avctx = avctx;
  129. s->flags = avctx->flags;
  130. if (!avctx->width || !avctx->height)
  131. return AVERROR_INVALIDDATA;
  132. s->width = avctx->width;
  133. s->height = avctx->height;
  134. // defaults
  135. s->num_h_slices = 1;
  136. s->num_v_slices = 1;
  137. return 0;
  138. }
  139. int ffv1_init_slice_state(FFV1Context *f, FFV1Context *fs)
  140. {
  141. int j;
  142. fs->plane_count = f->plane_count;
  143. fs->transparency = f->transparency;
  144. for (j = 0; j < f->plane_count; j++) {
  145. PlaneContext *const p = &fs->plane[j];
  146. if (fs->ac != AC_GOLOMB_RICE) {
  147. if (!p->state)
  148. p->state = av_malloc(CONTEXT_SIZE * p->context_count *
  149. sizeof(uint8_t));
  150. if (!p->state)
  151. return AVERROR(ENOMEM);
  152. } else {
  153. if (!p->vlc_state)
  154. p->vlc_state = av_malloc(p->context_count * sizeof(VlcState));
  155. if (!p->vlc_state)
  156. return AVERROR(ENOMEM);
  157. }
  158. }
  159. if (fs->ac == AC_RANGE_CUSTOM_TAB) {
  160. //FIXME only redo if state_transition changed
  161. for (j = 1; j < 256; j++) {
  162. fs->c.one_state[j] = f->state_transition[j];
  163. fs->c.zero_state[256 - j] = 256 - fs->c.one_state[j];
  164. }
  165. }
  166. return 0;
  167. }
  168. av_cold int ffv1_init_slice_contexts(FFV1Context *f)
  169. {
  170. int i, j;
  171. f->slice_count = f->num_h_slices * f->num_v_slices;
  172. if (f->slice_count <= 0) {
  173. av_log(f->avctx, AV_LOG_ERROR, "Invalid number of slices\n");
  174. return AVERROR(EINVAL);
  175. }
  176. for (i = 0; i < f->slice_count; i++) {
  177. int sx = i % f->num_h_slices;
  178. int sy = i / f->num_h_slices;
  179. int sxs = f->avctx->width * sx / f->num_h_slices;
  180. int sxe = f->avctx->width * (sx + 1) / f->num_h_slices;
  181. int sys = f->avctx->height * sy / f->num_v_slices;
  182. int sye = f->avctx->height * (sy + 1) / f->num_v_slices;
  183. FFV1Context *fs = av_mallocz(sizeof(*fs));
  184. if (!fs)
  185. goto memfail;
  186. f->slice_context[i] = fs;
  187. memcpy(fs, f, sizeof(*fs));
  188. memset(fs->rc_stat2, 0, sizeof(fs->rc_stat2));
  189. fs->slice_width = sxe - sxs;
  190. fs->slice_height = sye - sys;
  191. fs->slice_x = sxs;
  192. fs->slice_y = sys;
  193. fs->sample_buffer = av_malloc(3 * MAX_PLANES * (fs->width + 6) *
  194. sizeof(*fs->sample_buffer));
  195. if (!fs->sample_buffer) {
  196. av_free(fs);
  197. goto memfail;
  198. }
  199. }
  200. return 0;
  201. memfail:
  202. for (j = 0; j < i; j++) {
  203. av_free(f->slice_context[j]->sample_buffer);
  204. av_free(f->slice_context[j]);
  205. }
  206. return AVERROR(ENOMEM);
  207. }
  208. int ffv1_allocate_initial_states(FFV1Context *f)
  209. {
  210. int i;
  211. for (i = 0; i < f->quant_table_count; i++) {
  212. f->initial_states[i] = av_malloc(f->context_count[i] *
  213. sizeof(*f->initial_states[i]));
  214. if (!f->initial_states[i])
  215. return AVERROR(ENOMEM);
  216. memset(f->initial_states[i], 128,
  217. f->context_count[i] * sizeof(*f->initial_states[i]));
  218. }
  219. return 0;
  220. }
  221. void ffv1_clear_slice_state(FFV1Context *f, FFV1Context *fs)
  222. {
  223. int i, j;
  224. for (i = 0; i < f->plane_count; i++) {
  225. PlaneContext *p = &fs->plane[i];
  226. p->interlace_bit_state[0] = 128;
  227. p->interlace_bit_state[1] = 128;
  228. if (fs->ac != AC_GOLOMB_RICE) {
  229. if (f->initial_states[p->quant_table_index]) {
  230. memcpy(p->state, f->initial_states[p->quant_table_index],
  231. CONTEXT_SIZE * p->context_count);
  232. } else
  233. memset(p->state, 128, CONTEXT_SIZE * p->context_count);
  234. } else {
  235. for (j = 0; j < p->context_count; j++) {
  236. p->vlc_state[j].drift = 0;
  237. p->vlc_state[j].error_sum = 4; //FFMAX((RANGE + 32)/64, 2);
  238. p->vlc_state[j].bias = 0;
  239. p->vlc_state[j].count = 1;
  240. }
  241. }
  242. }
  243. }
  244. av_cold int ffv1_close(AVCodecContext *avctx)
  245. {
  246. FFV1Context *s = avctx->priv_data;
  247. int i, j;
  248. for (j = 0; j < s->slice_count; j++) {
  249. FFV1Context *fs = s->slice_context[j];
  250. for (i = 0; i < s->plane_count; i++) {
  251. PlaneContext *p = &fs->plane[i];
  252. av_freep(&p->state);
  253. av_freep(&p->vlc_state);
  254. }
  255. av_freep(&fs->sample_buffer);
  256. }
  257. av_freep(&avctx->stats_out);
  258. for (j = 0; j < s->quant_table_count; j++) {
  259. av_freep(&s->initial_states[j]);
  260. for (i = 0; i < s->slice_count; i++) {
  261. FFV1Context *sf = s->slice_context[i];
  262. av_freep(&sf->rc_stat2[j]);
  263. }
  264. av_freep(&s->rc_stat2[j]);
  265. }
  266. for (i = 0; i < s->slice_count; i++)
  267. av_freep(&s->slice_context[i]);
  268. return 0;
  269. }