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.

381 lines
12KB

  1. /*
  2. * Copyright (c) 2003 Michael Niedermayer
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * ASUS V1/V2 encoder.
  23. */
  24. #include "libavutil/attributes.h"
  25. #include "libavutil/mem.h"
  26. #include "aandcttab.h"
  27. #include "asv.h"
  28. #include "avcodec.h"
  29. #include "dct.h"
  30. #include "fdctdsp.h"
  31. #include "internal.h"
  32. #include "mathops.h"
  33. #include "mpeg12data.h"
  34. static inline void asv2_put_bits(PutBitContext *pb, int n, int v)
  35. {
  36. put_bits(pb, n, ff_reverse[v << (8 - n)]);
  37. }
  38. static inline void asv1_put_level(PutBitContext *pb, int level)
  39. {
  40. unsigned int index = level + 3;
  41. if (index <= 6) {
  42. put_bits(pb, ff_asv_level_tab[index][1], ff_asv_level_tab[index][0]);
  43. } else {
  44. put_bits(pb, ff_asv_level_tab[3][1], ff_asv_level_tab[3][0]);
  45. put_sbits(pb, 8, level);
  46. }
  47. }
  48. static inline void asv2_put_level(ASV1Context *a, PutBitContext *pb, int level)
  49. {
  50. unsigned int index = level + 31;
  51. if (index <= 62) {
  52. put_bits(pb, ff_asv2_level_tab[index][1], ff_asv2_level_tab[index][0]);
  53. } else {
  54. put_bits(pb, ff_asv2_level_tab[31][1], ff_asv2_level_tab[31][0]);
  55. if (level < -128 || level > 127) {
  56. av_log(a->avctx, AV_LOG_WARNING, "Clipping level %d, increase qscale\n", level);
  57. level = av_clip_int8(level);
  58. }
  59. asv2_put_bits(pb, 8, level & 0xFF);
  60. }
  61. }
  62. static inline void asv1_encode_block(ASV1Context *a, int16_t block[64])
  63. {
  64. int i;
  65. int nc_count = 0;
  66. put_bits(&a->pb, 8, (block[0] + 32) >> 6);
  67. block[0] = 0;
  68. for (i = 0; i < 10; i++) {
  69. const int index = ff_asv_scantab[4 * i];
  70. int ccp = 0;
  71. if ((block[index + 0] = (block[index + 0] *
  72. a->q_intra_matrix[index + 0] + (1 << 15)) >> 16))
  73. ccp |= 8;
  74. if ((block[index + 8] = (block[index + 8] *
  75. a->q_intra_matrix[index + 8] + (1 << 15)) >> 16))
  76. ccp |= 4;
  77. if ((block[index + 1] = (block[index + 1] *
  78. a->q_intra_matrix[index + 1] + (1 << 15)) >> 16))
  79. ccp |= 2;
  80. if ((block[index + 9] = (block[index + 9] *
  81. a->q_intra_matrix[index + 9] + (1 << 15)) >> 16))
  82. ccp |= 1;
  83. if (ccp) {
  84. for (; nc_count; nc_count--)
  85. put_bits(&a->pb, ff_asv_ccp_tab[0][1], ff_asv_ccp_tab[0][0]);
  86. put_bits(&a->pb, ff_asv_ccp_tab[ccp][1], ff_asv_ccp_tab[ccp][0]);
  87. if (ccp & 8)
  88. asv1_put_level(&a->pb, block[index + 0]);
  89. if (ccp & 4)
  90. asv1_put_level(&a->pb, block[index + 8]);
  91. if (ccp & 2)
  92. asv1_put_level(&a->pb, block[index + 1]);
  93. if (ccp & 1)
  94. asv1_put_level(&a->pb, block[index + 9]);
  95. } else {
  96. nc_count++;
  97. }
  98. }
  99. put_bits(&a->pb, ff_asv_ccp_tab[16][1], ff_asv_ccp_tab[16][0]);
  100. }
  101. static inline void asv2_encode_block(ASV1Context *a, int16_t block[64])
  102. {
  103. int i;
  104. int count = 0;
  105. for (count = 63; count > 3; count--) {
  106. const int index = ff_asv_scantab[count];
  107. if ((block[index] * a->q_intra_matrix[index] + (1 << 15)) >> 16)
  108. break;
  109. }
  110. count >>= 2;
  111. asv2_put_bits(&a->pb, 4, count);
  112. asv2_put_bits(&a->pb, 8, (block[0] + 32) >> 6);
  113. block[0] = 0;
  114. for (i = 0; i <= count; i++) {
  115. const int index = ff_asv_scantab[4 * i];
  116. int ccp = 0;
  117. if ((block[index + 0] = (block[index + 0] *
  118. a->q_intra_matrix[index + 0] + (1 << 15)) >> 16))
  119. ccp |= 8;
  120. if ((block[index + 8] = (block[index + 8] *
  121. a->q_intra_matrix[index + 8] + (1 << 15)) >> 16))
  122. ccp |= 4;
  123. if ((block[index + 1] = (block[index + 1] *
  124. a->q_intra_matrix[index + 1] + (1 << 15)) >> 16))
  125. ccp |= 2;
  126. if ((block[index + 9] = (block[index + 9] *
  127. a->q_intra_matrix[index + 9] + (1 << 15)) >> 16))
  128. ccp |= 1;
  129. av_assert2(i || ccp < 8);
  130. if (i)
  131. put_bits(&a->pb, ff_asv_ac_ccp_tab[ccp][1], ff_asv_ac_ccp_tab[ccp][0]);
  132. else
  133. put_bits(&a->pb, ff_asv_dc_ccp_tab[ccp][1], ff_asv_dc_ccp_tab[ccp][0]);
  134. if (ccp) {
  135. if (ccp & 8)
  136. asv2_put_level(a, &a->pb, block[index + 0]);
  137. if (ccp & 4)
  138. asv2_put_level(a, &a->pb, block[index + 8]);
  139. if (ccp & 2)
  140. asv2_put_level(a, &a->pb, block[index + 1]);
  141. if (ccp & 1)
  142. asv2_put_level(a, &a->pb, block[index + 9]);
  143. }
  144. }
  145. }
  146. #define MAX_MB_SIZE (30 * 16 * 16 * 3 / 2 / 8)
  147. static inline int encode_mb(ASV1Context *a, int16_t block[6][64])
  148. {
  149. int i;
  150. av_assert0(a->pb.buf_end - a->pb.buf - (put_bits_count(&a->pb) >> 3) >= MAX_MB_SIZE);
  151. if (a->avctx->codec_id == AV_CODEC_ID_ASV1) {
  152. for (i = 0; i < 6; i++)
  153. asv1_encode_block(a, block[i]);
  154. } else {
  155. for (i = 0; i < 6; i++) {
  156. asv2_encode_block(a, block[i]);
  157. }
  158. }
  159. return 0;
  160. }
  161. static inline void dct_get(ASV1Context *a, const AVFrame *frame,
  162. int mb_x, int mb_y)
  163. {
  164. int16_t (*block)[64] = a->block;
  165. int linesize = frame->linesize[0];
  166. int i;
  167. uint8_t *ptr_y = frame->data[0] + (mb_y * 16 * linesize) + mb_x * 16;
  168. uint8_t *ptr_cb = frame->data[1] + (mb_y * 8 * frame->linesize[1]) + mb_x * 8;
  169. uint8_t *ptr_cr = frame->data[2] + (mb_y * 8 * frame->linesize[2]) + mb_x * 8;
  170. a->pdsp.get_pixels(block[0], ptr_y, linesize);
  171. a->pdsp.get_pixels(block[1], ptr_y + 8, linesize);
  172. a->pdsp.get_pixels(block[2], ptr_y + 8 * linesize, linesize);
  173. a->pdsp.get_pixels(block[3], ptr_y + 8 * linesize + 8, linesize);
  174. for (i = 0; i < 4; i++)
  175. a->fdsp.fdct(block[i]);
  176. if (!(a->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  177. a->pdsp.get_pixels(block[4], ptr_cb, frame->linesize[1]);
  178. a->pdsp.get_pixels(block[5], ptr_cr, frame->linesize[2]);
  179. for (i = 4; i < 6; i++)
  180. a->fdsp.fdct(block[i]);
  181. }
  182. }
  183. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  184. const AVFrame *pict, int *got_packet)
  185. {
  186. ASV1Context *const a = avctx->priv_data;
  187. int size, ret;
  188. int mb_x, mb_y;
  189. if (pict->width % 16 || pict->height % 16) {
  190. AVFrame *clone = av_frame_alloc();
  191. int i;
  192. if (!clone)
  193. return AVERROR(ENOMEM);
  194. clone->format = pict->format;
  195. clone->width = FFALIGN(pict->width, 16);
  196. clone->height = FFALIGN(pict->height, 16);
  197. ret = av_frame_get_buffer(clone, 0);
  198. if (ret < 0) {
  199. av_frame_free(&clone);
  200. return ret;
  201. }
  202. ret = av_frame_copy(clone, pict);
  203. if (ret < 0) {
  204. av_frame_free(&clone);
  205. return ret;
  206. }
  207. for (i = 0; i<3; i++) {
  208. int x, y;
  209. int w = AV_CEIL_RSHIFT(pict->width, !!i);
  210. int h = AV_CEIL_RSHIFT(pict->height, !!i);
  211. int w2 = AV_CEIL_RSHIFT(clone->width, !!i);
  212. int h2 = AV_CEIL_RSHIFT(clone->height, !!i);
  213. for (y=0; y<h; y++)
  214. for (x=w; x<w2; x++)
  215. clone->data[i][x + y*clone->linesize[i]] =
  216. clone->data[i][w - 1 + y*clone->linesize[i]];
  217. for (y=h; y<h2; y++)
  218. for (x=0; x<w2; x++)
  219. clone->data[i][x + y*clone->linesize[i]] =
  220. clone->data[i][x + (h-1)*clone->linesize[i]];
  221. }
  222. ret = encode_frame(avctx, pkt, clone, got_packet);
  223. av_frame_free(&clone);
  224. return ret;
  225. }
  226. if ((ret = ff_alloc_packet2(avctx, pkt, a->mb_height * a->mb_width * MAX_MB_SIZE +
  227. AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
  228. return ret;
  229. init_put_bits(&a->pb, pkt->data, pkt->size);
  230. for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
  231. for (mb_x = 0; mb_x < a->mb_width2; mb_x++) {
  232. dct_get(a, pict, mb_x, mb_y);
  233. encode_mb(a, a->block);
  234. }
  235. }
  236. if (a->mb_width2 != a->mb_width) {
  237. mb_x = a->mb_width2;
  238. for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
  239. dct_get(a, pict, mb_x, mb_y);
  240. encode_mb(a, a->block);
  241. }
  242. }
  243. if (a->mb_height2 != a->mb_height) {
  244. mb_y = a->mb_height2;
  245. for (mb_x = 0; mb_x < a->mb_width; mb_x++) {
  246. dct_get(a, pict, mb_x, mb_y);
  247. encode_mb(a, a->block);
  248. }
  249. }
  250. emms_c();
  251. avpriv_align_put_bits(&a->pb);
  252. while (put_bits_count(&a->pb) & 31)
  253. put_bits(&a->pb, 8, 0);
  254. size = put_bits_count(&a->pb) / 32;
  255. if (avctx->codec_id == AV_CODEC_ID_ASV1) {
  256. a->bbdsp.bswap_buf((uint32_t *) pkt->data,
  257. (uint32_t *) pkt->data, size);
  258. } else {
  259. int i;
  260. for (i = 0; i < 4 * size; i++)
  261. pkt->data[i] = ff_reverse[pkt->data[i]];
  262. }
  263. pkt->size = size * 4;
  264. pkt->flags |= AV_PKT_FLAG_KEY;
  265. *got_packet = 1;
  266. return 0;
  267. }
  268. static av_cold int encode_init(AVCodecContext *avctx)
  269. {
  270. ASV1Context *const a = avctx->priv_data;
  271. int i;
  272. const int scale = avctx->codec_id == AV_CODEC_ID_ASV1 ? 1 : 2;
  273. ff_asv_common_init(avctx);
  274. ff_fdctdsp_init(&a->fdsp, avctx);
  275. ff_pixblockdsp_init(&a->pdsp, avctx);
  276. if (avctx->global_quality <= 0)
  277. avctx->global_quality = 4 * FF_QUALITY_SCALE;
  278. a->inv_qscale = (32 * scale * FF_QUALITY_SCALE +
  279. avctx->global_quality / 2) / avctx->global_quality;
  280. avctx->extradata = av_mallocz(8);
  281. if (!avctx->extradata)
  282. return AVERROR(ENOMEM);
  283. avctx->extradata_size = 8;
  284. ((uint32_t *) avctx->extradata)[0] = av_le2ne32(a->inv_qscale);
  285. ((uint32_t *) avctx->extradata)[1] = av_le2ne32(AV_RL32("ASUS"));
  286. for (i = 0; i < 64; i++) {
  287. if (a->fdsp.fdct == ff_fdct_ifast) {
  288. int q = 32LL * scale * ff_mpeg1_default_intra_matrix[i] * ff_aanscales[i];
  289. a->q_intra_matrix[i] = (((int64_t)a->inv_qscale << 30) + q / 2) / q;
  290. } else {
  291. int q = 32 * scale * ff_mpeg1_default_intra_matrix[i];
  292. a->q_intra_matrix[i] = ((a->inv_qscale << 16) + q / 2) / q;
  293. }
  294. }
  295. return 0;
  296. }
  297. #if CONFIG_ASV1_ENCODER
  298. AVCodec ff_asv1_encoder = {
  299. .name = "asv1",
  300. .long_name = NULL_IF_CONFIG_SMALL("ASUS V1"),
  301. .type = AVMEDIA_TYPE_VIDEO,
  302. .id = AV_CODEC_ID_ASV1,
  303. .priv_data_size = sizeof(ASV1Context),
  304. .init = encode_init,
  305. .encode2 = encode_frame,
  306. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  307. AV_PIX_FMT_NONE },
  308. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  309. };
  310. #endif
  311. #if CONFIG_ASV2_ENCODER
  312. AVCodec ff_asv2_encoder = {
  313. .name = "asv2",
  314. .long_name = NULL_IF_CONFIG_SMALL("ASUS V2"),
  315. .type = AVMEDIA_TYPE_VIDEO,
  316. .id = AV_CODEC_ID_ASV2,
  317. .priv_data_size = sizeof(ASV1Context),
  318. .init = encode_init,
  319. .encode2 = encode_frame,
  320. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  321. AV_PIX_FMT_NONE },
  322. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  323. };
  324. #endif