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.

372 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 "mpeg12data.h"
  33. static inline void asv1_put_level(PutBitContext *pb, int level)
  34. {
  35. unsigned int index = level + 3;
  36. if (index <= 6) {
  37. put_bits(pb, ff_asv_level_tab[index][1], ff_asv_level_tab[index][0]);
  38. } else {
  39. put_bits(pb, 3, 0); /* Escape code */
  40. put_sbits(pb, 8, level);
  41. }
  42. }
  43. static inline void asv2_put_level(ASV1Context *a, PutBitContext *pb, int level)
  44. {
  45. unsigned int index = level + 31;
  46. if (index <= 62) {
  47. put_bits_le(pb, ff_asv2_level_tab[index][1], ff_asv2_level_tab[index][0]);
  48. } else {
  49. put_bits_le(pb, 5, 0); /* Escape code */
  50. if (level < -128 || level > 127) {
  51. av_log(a->avctx, AV_LOG_WARNING, "Clipping level %d, increase qscale\n", level);
  52. level = av_clip_int8(level);
  53. }
  54. put_bits_le(pb, 8, level & 0xFF);
  55. }
  56. }
  57. static inline void asv1_encode_block(ASV1Context *a, int16_t block[64])
  58. {
  59. int i;
  60. int nc_count = 0;
  61. put_bits(&a->pb, 8, (block[0] + 32) >> 6);
  62. block[0] = 0;
  63. for (i = 0; i < 10; i++) {
  64. const int index = ff_asv_scantab[4 * i];
  65. int ccp = 0;
  66. if ((block[index + 0] = (block[index + 0] *
  67. a->q_intra_matrix[index + 0] + (1 << 15)) >> 16))
  68. ccp |= 8;
  69. if ((block[index + 8] = (block[index + 8] *
  70. a->q_intra_matrix[index + 8] + (1 << 15)) >> 16))
  71. ccp |= 4;
  72. if ((block[index + 1] = (block[index + 1] *
  73. a->q_intra_matrix[index + 1] + (1 << 15)) >> 16))
  74. ccp |= 2;
  75. if ((block[index + 9] = (block[index + 9] *
  76. a->q_intra_matrix[index + 9] + (1 << 15)) >> 16))
  77. ccp |= 1;
  78. if (ccp) {
  79. for (; nc_count; nc_count--)
  80. put_bits(&a->pb, 2, 2); /* Skip */
  81. put_bits(&a->pb, ff_asv_ccp_tab[ccp][1], ff_asv_ccp_tab[ccp][0]);
  82. if (ccp & 8)
  83. asv1_put_level(&a->pb, block[index + 0]);
  84. if (ccp & 4)
  85. asv1_put_level(&a->pb, block[index + 8]);
  86. if (ccp & 2)
  87. asv1_put_level(&a->pb, block[index + 1]);
  88. if (ccp & 1)
  89. asv1_put_level(&a->pb, block[index + 9]);
  90. } else {
  91. nc_count++;
  92. }
  93. }
  94. put_bits(&a->pb, 5, 0xF); /* End of block */
  95. }
  96. static inline void asv2_encode_block(ASV1Context *a, int16_t block[64])
  97. {
  98. int i;
  99. int count = 0;
  100. for (count = 63; count > 3; count--) {
  101. const int index = ff_asv_scantab[count];
  102. if ((block[index] * a->q_intra_matrix[index] + (1 << 15)) >> 16)
  103. break;
  104. }
  105. count >>= 2;
  106. put_bits_le(&a->pb, 4, count);
  107. put_bits_le(&a->pb, 8, (block[0] + 32) >> 6);
  108. block[0] = 0;
  109. for (i = 0; i <= count; i++) {
  110. const int index = ff_asv_scantab[4 * i];
  111. int ccp = 0;
  112. if ((block[index + 0] = (block[index + 0] *
  113. a->q_intra_matrix[index + 0] + (1 << 15)) >> 16))
  114. ccp |= 8;
  115. if ((block[index + 8] = (block[index + 8] *
  116. a->q_intra_matrix[index + 8] + (1 << 15)) >> 16))
  117. ccp |= 4;
  118. if ((block[index + 1] = (block[index + 1] *
  119. a->q_intra_matrix[index + 1] + (1 << 15)) >> 16))
  120. ccp |= 2;
  121. if ((block[index + 9] = (block[index + 9] *
  122. a->q_intra_matrix[index + 9] + (1 << 15)) >> 16))
  123. ccp |= 1;
  124. av_assert2(i || ccp < 8);
  125. if (i)
  126. put_bits_le(&a->pb, ff_asv_ac_ccp_tab[ccp][1], ff_asv_ac_ccp_tab[ccp][0]);
  127. else
  128. put_bits_le(&a->pb, ff_asv_dc_ccp_tab[ccp][1], ff_asv_dc_ccp_tab[ccp][0]);
  129. if (ccp) {
  130. if (ccp & 8)
  131. asv2_put_level(a, &a->pb, block[index + 0]);
  132. if (ccp & 4)
  133. asv2_put_level(a, &a->pb, block[index + 8]);
  134. if (ccp & 2)
  135. asv2_put_level(a, &a->pb, block[index + 1]);
  136. if (ccp & 1)
  137. asv2_put_level(a, &a->pb, block[index + 9]);
  138. }
  139. }
  140. }
  141. #define MAX_MB_SIZE (30 * 16 * 16 * 3 / 2 / 8)
  142. static inline int encode_mb(ASV1Context *a, int16_t block[6][64])
  143. {
  144. int i;
  145. av_assert0(a->pb.buf_end - a->pb.buf - (put_bits_count(&a->pb) >> 3) >= MAX_MB_SIZE);
  146. if (a->avctx->codec_id == AV_CODEC_ID_ASV1) {
  147. for (i = 0; i < 6; i++)
  148. asv1_encode_block(a, block[i]);
  149. } else {
  150. for (i = 0; i < 6; i++) {
  151. asv2_encode_block(a, block[i]);
  152. }
  153. }
  154. return 0;
  155. }
  156. static inline void dct_get(ASV1Context *a, const AVFrame *frame,
  157. int mb_x, int mb_y)
  158. {
  159. int16_t (*block)[64] = a->block;
  160. int linesize = frame->linesize[0];
  161. int i;
  162. uint8_t *ptr_y = frame->data[0] + (mb_y * 16 * linesize) + mb_x * 16;
  163. uint8_t *ptr_cb = frame->data[1] + (mb_y * 8 * frame->linesize[1]) + mb_x * 8;
  164. uint8_t *ptr_cr = frame->data[2] + (mb_y * 8 * frame->linesize[2]) + mb_x * 8;
  165. a->pdsp.get_pixels(block[0], ptr_y, linesize);
  166. a->pdsp.get_pixels(block[1], ptr_y + 8, linesize);
  167. a->pdsp.get_pixels(block[2], ptr_y + 8 * linesize, linesize);
  168. a->pdsp.get_pixels(block[3], ptr_y + 8 * linesize + 8, linesize);
  169. for (i = 0; i < 4; i++)
  170. a->fdsp.fdct(block[i]);
  171. if (!(a->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  172. a->pdsp.get_pixels(block[4], ptr_cb, frame->linesize[1]);
  173. a->pdsp.get_pixels(block[5], ptr_cr, frame->linesize[2]);
  174. for (i = 4; i < 6; i++)
  175. a->fdsp.fdct(block[i]);
  176. }
  177. }
  178. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  179. const AVFrame *pict, int *got_packet)
  180. {
  181. ASV1Context *const a = avctx->priv_data;
  182. int size, ret;
  183. int mb_x, mb_y;
  184. if (pict->width % 16 || pict->height % 16) {
  185. AVFrame *clone = av_frame_alloc();
  186. int i;
  187. if (!clone)
  188. return AVERROR(ENOMEM);
  189. clone->format = pict->format;
  190. clone->width = FFALIGN(pict->width, 16);
  191. clone->height = FFALIGN(pict->height, 16);
  192. ret = av_frame_get_buffer(clone, 0);
  193. if (ret < 0) {
  194. av_frame_free(&clone);
  195. return ret;
  196. }
  197. ret = av_frame_copy(clone, pict);
  198. if (ret < 0) {
  199. av_frame_free(&clone);
  200. return ret;
  201. }
  202. for (i = 0; i<3; i++) {
  203. int x, y;
  204. int w = AV_CEIL_RSHIFT(pict->width, !!i);
  205. int h = AV_CEIL_RSHIFT(pict->height, !!i);
  206. int w2 = AV_CEIL_RSHIFT(clone->width, !!i);
  207. int h2 = AV_CEIL_RSHIFT(clone->height, !!i);
  208. for (y=0; y<h; y++)
  209. for (x=w; x<w2; x++)
  210. clone->data[i][x + y*clone->linesize[i]] =
  211. clone->data[i][w - 1 + y*clone->linesize[i]];
  212. for (y=h; y<h2; y++)
  213. for (x=0; x<w2; x++)
  214. clone->data[i][x + y*clone->linesize[i]] =
  215. clone->data[i][x + (h-1)*clone->linesize[i]];
  216. }
  217. ret = encode_frame(avctx, pkt, clone, got_packet);
  218. av_frame_free(&clone);
  219. return ret;
  220. }
  221. if ((ret = ff_alloc_packet2(avctx, pkt, a->mb_height * a->mb_width * MAX_MB_SIZE +
  222. AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
  223. return ret;
  224. init_put_bits(&a->pb, pkt->data, pkt->size);
  225. for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
  226. for (mb_x = 0; mb_x < a->mb_width2; mb_x++) {
  227. dct_get(a, pict, mb_x, mb_y);
  228. encode_mb(a, a->block);
  229. }
  230. }
  231. if (a->mb_width2 != a->mb_width) {
  232. mb_x = a->mb_width2;
  233. for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
  234. dct_get(a, pict, mb_x, mb_y);
  235. encode_mb(a, a->block);
  236. }
  237. }
  238. if (a->mb_height2 != a->mb_height) {
  239. mb_y = a->mb_height2;
  240. for (mb_x = 0; mb_x < a->mb_width; mb_x++) {
  241. dct_get(a, pict, mb_x, mb_y);
  242. encode_mb(a, a->block);
  243. }
  244. }
  245. emms_c();
  246. if (avctx->codec_id == AV_CODEC_ID_ASV1)
  247. flush_put_bits(&a->pb);
  248. else
  249. flush_put_bits_le(&a->pb);
  250. AV_WN32(put_bits_ptr(&a->pb), 0);
  251. size = (put_bits_count(&a->pb) + 31) / 32;
  252. if (avctx->codec_id == AV_CODEC_ID_ASV1) {
  253. a->bbdsp.bswap_buf((uint32_t *) pkt->data,
  254. (uint32_t *) pkt->data, size);
  255. }
  256. pkt->size = size * 4;
  257. pkt->flags |= AV_PKT_FLAG_KEY;
  258. *got_packet = 1;
  259. return 0;
  260. }
  261. static av_cold int encode_init(AVCodecContext *avctx)
  262. {
  263. ASV1Context *const a = avctx->priv_data;
  264. int i;
  265. const int scale = avctx->codec_id == AV_CODEC_ID_ASV1 ? 1 : 2;
  266. ff_asv_common_init(avctx);
  267. ff_fdctdsp_init(&a->fdsp, avctx);
  268. ff_pixblockdsp_init(&a->pdsp, avctx);
  269. if (avctx->global_quality <= 0)
  270. avctx->global_quality = 4 * FF_QUALITY_SCALE;
  271. a->inv_qscale = (32 * scale * FF_QUALITY_SCALE +
  272. avctx->global_quality / 2) / avctx->global_quality;
  273. avctx->extradata = av_mallocz(8);
  274. if (!avctx->extradata)
  275. return AVERROR(ENOMEM);
  276. avctx->extradata_size = 8;
  277. ((uint32_t *) avctx->extradata)[0] = av_le2ne32(a->inv_qscale);
  278. ((uint32_t *) avctx->extradata)[1] = av_le2ne32(AV_RL32("ASUS"));
  279. for (i = 0; i < 64; i++) {
  280. if (a->fdsp.fdct == ff_fdct_ifast) {
  281. int q = 32LL * scale * ff_mpeg1_default_intra_matrix[i] * ff_aanscales[i];
  282. a->q_intra_matrix[i] = (((int64_t)a->inv_qscale << 30) + q / 2) / q;
  283. } else {
  284. int q = 32 * scale * ff_mpeg1_default_intra_matrix[i];
  285. a->q_intra_matrix[i] = ((a->inv_qscale << 16) + q / 2) / q;
  286. }
  287. }
  288. return 0;
  289. }
  290. #if CONFIG_ASV1_ENCODER
  291. AVCodec ff_asv1_encoder = {
  292. .name = "asv1",
  293. .long_name = NULL_IF_CONFIG_SMALL("ASUS V1"),
  294. .type = AVMEDIA_TYPE_VIDEO,
  295. .id = AV_CODEC_ID_ASV1,
  296. .priv_data_size = sizeof(ASV1Context),
  297. .init = encode_init,
  298. .encode2 = encode_frame,
  299. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  300. AV_PIX_FMT_NONE },
  301. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  302. };
  303. #endif
  304. #if CONFIG_ASV2_ENCODER
  305. AVCodec ff_asv2_encoder = {
  306. .name = "asv2",
  307. .long_name = NULL_IF_CONFIG_SMALL("ASUS V2"),
  308. .type = AVMEDIA_TYPE_VIDEO,
  309. .id = AV_CODEC_ID_ASV2,
  310. .priv_data_size = sizeof(ASV1Context),
  311. .init = encode_init,
  312. .encode2 = encode_frame,
  313. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  314. AV_PIX_FMT_NONE },
  315. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
  316. };
  317. #endif