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.

384 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, "Cliping 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. if (a->pb.buf_end - a->pb.buf - (put_bits_count(&a->pb) >> 3) < MAX_MB_SIZE) {
  151. av_log(a->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  152. return -1;
  153. }
  154. if (a->avctx->codec_id == AV_CODEC_ID_ASV1) {
  155. for (i = 0; i < 6; i++)
  156. asv1_encode_block(a, block[i]);
  157. } else {
  158. for (i = 0; i < 6; i++) {
  159. asv2_encode_block(a, block[i]);
  160. }
  161. }
  162. return 0;
  163. }
  164. static inline void dct_get(ASV1Context *a, const AVFrame *frame,
  165. int mb_x, int mb_y)
  166. {
  167. int16_t (*block)[64] = a->block;
  168. int linesize = frame->linesize[0];
  169. int i;
  170. uint8_t *ptr_y = frame->data[0] + (mb_y * 16 * linesize) + mb_x * 16;
  171. uint8_t *ptr_cb = frame->data[1] + (mb_y * 8 * frame->linesize[1]) + mb_x * 8;
  172. uint8_t *ptr_cr = frame->data[2] + (mb_y * 8 * frame->linesize[2]) + mb_x * 8;
  173. a->pdsp.get_pixels(block[0], ptr_y, linesize);
  174. a->pdsp.get_pixels(block[1], ptr_y + 8, linesize);
  175. a->pdsp.get_pixels(block[2], ptr_y + 8 * linesize, linesize);
  176. a->pdsp.get_pixels(block[3], ptr_y + 8 * linesize + 8, linesize);
  177. for (i = 0; i < 4; i++)
  178. a->fdsp.fdct(block[i]);
  179. if (!(a->avctx->flags & CODEC_FLAG_GRAY)) {
  180. a->pdsp.get_pixels(block[4], ptr_cb, frame->linesize[1]);
  181. a->pdsp.get_pixels(block[5], ptr_cr, frame->linesize[2]);
  182. for (i = 4; i < 6; i++)
  183. a->fdsp.fdct(block[i]);
  184. }
  185. }
  186. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  187. const AVFrame *pict, int *got_packet)
  188. {
  189. ASV1Context *const a = avctx->priv_data;
  190. int size, ret;
  191. int mb_x, mb_y;
  192. if (pict->width % 16 || pict->height % 16) {
  193. AVFrame *clone = av_frame_alloc();
  194. int i;
  195. if (!clone)
  196. return AVERROR(ENOMEM);
  197. clone->format = pict->format;
  198. clone->width = FFALIGN(pict->width, 16);
  199. clone->height = FFALIGN(pict->height, 16);
  200. ret = av_frame_get_buffer(clone, 32);
  201. if (ret < 0) {
  202. av_frame_free(&clone);
  203. return ret;
  204. }
  205. ret = av_frame_copy(clone, pict);
  206. if (ret < 0) {
  207. av_frame_free(&clone);
  208. return ret;
  209. }
  210. for (i = 0; i<3; i++) {
  211. int x, y;
  212. int w = FF_CEIL_RSHIFT(pict->width, !!i);
  213. int h = FF_CEIL_RSHIFT(pict->height, !!i);
  214. int w2 = FF_CEIL_RSHIFT(clone->width, !!i);
  215. int h2 = FF_CEIL_RSHIFT(clone->height, !!i);
  216. for (y=0; y<h; y++)
  217. for (x=w; x<w2; x++)
  218. clone->data[i][x + y*clone->linesize[i]] =
  219. clone->data[i][w - 1 + y*clone->linesize[i]];
  220. for (y=h; y<h2; y++)
  221. for (x=0; x<w2; x++)
  222. clone->data[i][x + y*clone->linesize[i]] =
  223. clone->data[i][x + (h-1)*clone->linesize[i]];
  224. }
  225. ret = encode_frame(avctx, pkt, clone, got_packet);
  226. av_frame_free(&clone);
  227. return ret;
  228. }
  229. if ((ret = ff_alloc_packet2(avctx, pkt, a->mb_height * a->mb_width * MAX_MB_SIZE +
  230. FF_MIN_BUFFER_SIZE, 0)) < 0)
  231. return ret;
  232. init_put_bits(&a->pb, pkt->data, pkt->size);
  233. for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
  234. for (mb_x = 0; mb_x < a->mb_width2; mb_x++) {
  235. dct_get(a, pict, mb_x, mb_y);
  236. encode_mb(a, a->block);
  237. }
  238. }
  239. if (a->mb_width2 != a->mb_width) {
  240. mb_x = a->mb_width2;
  241. for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
  242. dct_get(a, pict, mb_x, mb_y);
  243. encode_mb(a, a->block);
  244. }
  245. }
  246. if (a->mb_height2 != a->mb_height) {
  247. mb_y = a->mb_height2;
  248. for (mb_x = 0; mb_x < a->mb_width; mb_x++) {
  249. dct_get(a, pict, mb_x, mb_y);
  250. encode_mb(a, a->block);
  251. }
  252. }
  253. emms_c();
  254. avpriv_align_put_bits(&a->pb);
  255. while (put_bits_count(&a->pb) & 31)
  256. put_bits(&a->pb, 8, 0);
  257. size = put_bits_count(&a->pb) / 32;
  258. if (avctx->codec_id == AV_CODEC_ID_ASV1) {
  259. a->bbdsp.bswap_buf((uint32_t *) pkt->data,
  260. (uint32_t *) pkt->data, size);
  261. } else {
  262. int i;
  263. for (i = 0; i < 4 * size; i++)
  264. pkt->data[i] = ff_reverse[pkt->data[i]];
  265. }
  266. pkt->size = size * 4;
  267. pkt->flags |= AV_PKT_FLAG_KEY;
  268. *got_packet = 1;
  269. return 0;
  270. }
  271. static av_cold int encode_init(AVCodecContext *avctx)
  272. {
  273. ASV1Context *const a = avctx->priv_data;
  274. int i;
  275. const int scale = avctx->codec_id == AV_CODEC_ID_ASV1 ? 1 : 2;
  276. ff_asv_common_init(avctx);
  277. ff_fdctdsp_init(&a->fdsp, avctx);
  278. ff_pixblockdsp_init(&a->pdsp, avctx);
  279. if (avctx->global_quality <= 0)
  280. avctx->global_quality = 4 * FF_QUALITY_SCALE;
  281. a->inv_qscale = (32 * scale * FF_QUALITY_SCALE +
  282. avctx->global_quality / 2) / avctx->global_quality;
  283. avctx->extradata = av_mallocz(8);
  284. if (!avctx->extradata)
  285. return AVERROR(ENOMEM);
  286. avctx->extradata_size = 8;
  287. ((uint32_t *) avctx->extradata)[0] = av_le2ne32(a->inv_qscale);
  288. ((uint32_t *) avctx->extradata)[1] = av_le2ne32(AV_RL32("ASUS"));
  289. for (i = 0; i < 64; i++) {
  290. if (a->fdsp.fdct == ff_fdct_ifast) {
  291. int q = 32LL * scale * ff_mpeg1_default_intra_matrix[i] * ff_aanscales[i];
  292. a->q_intra_matrix[i] = (((int64_t)a->inv_qscale << 30) + q / 2) / q;
  293. } else {
  294. int q = 32 * scale * ff_mpeg1_default_intra_matrix[i];
  295. a->q_intra_matrix[i] = ((a->inv_qscale << 16) + q / 2) / q;
  296. }
  297. }
  298. return 0;
  299. }
  300. #if CONFIG_ASV1_ENCODER
  301. AVCodec ff_asv1_encoder = {
  302. .name = "asv1",
  303. .long_name = NULL_IF_CONFIG_SMALL("ASUS V1"),
  304. .type = AVMEDIA_TYPE_VIDEO,
  305. .id = AV_CODEC_ID_ASV1,
  306. .priv_data_size = sizeof(ASV1Context),
  307. .init = encode_init,
  308. .encode2 = encode_frame,
  309. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  310. AV_PIX_FMT_NONE },
  311. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  312. };
  313. #endif
  314. #if CONFIG_ASV2_ENCODER
  315. AVCodec ff_asv2_encoder = {
  316. .name = "asv2",
  317. .long_name = NULL_IF_CONFIG_SMALL("ASUS V2"),
  318. .type = AVMEDIA_TYPE_VIDEO,
  319. .id = AV_CODEC_ID_ASV2,
  320. .priv_data_size = sizeof(ASV1Context),
  321. .init = encode_init,
  322. .encode2 = encode_frame,
  323. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  324. AV_PIX_FMT_NONE },
  325. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  326. };
  327. #endif