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.

343 lines
11KB

  1. /*
  2. * Copyright (c) 2003 Michael Niedermayer
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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 "asv.h"
  27. #include "avcodec.h"
  28. #include "fdctdsp.h"
  29. #include "mathops.h"
  30. #include "mpeg12data.h"
  31. static inline void asv2_put_bits(PutBitContext *pb, int n, int v)
  32. {
  33. put_bits(pb, n, ff_reverse[v << (8 - n)]);
  34. }
  35. static inline void asv1_put_level(PutBitContext *pb, int level)
  36. {
  37. unsigned int index = level + 3;
  38. if (index <= 6) {
  39. put_bits(pb, ff_asv_level_tab[index][1], ff_asv_level_tab[index][0]);
  40. } else {
  41. put_bits(pb, ff_asv_level_tab[3][1], ff_asv_level_tab[3][0]);
  42. put_sbits(pb, 8, level);
  43. }
  44. }
  45. static inline void asv2_put_level(PutBitContext *pb, int level)
  46. {
  47. unsigned int index = level + 31;
  48. if (index <= 62) {
  49. put_bits(pb, ff_asv2_level_tab[index][1], ff_asv2_level_tab[index][0]);
  50. } else {
  51. put_bits(pb, ff_asv2_level_tab[31][1], ff_asv2_level_tab[31][0]);
  52. asv2_put_bits(pb, 8, level & 0xFF);
  53. }
  54. }
  55. static inline void asv1_encode_block(ASV1Context *a, int16_t block[64])
  56. {
  57. int i;
  58. int nc_count = 0;
  59. put_bits(&a->pb, 8, (block[0] + 32) >> 6);
  60. block[0] = 0;
  61. for (i = 0; i < 10; i++) {
  62. const int index = ff_asv_scantab[4 * i];
  63. int ccp = 0;
  64. if ((block[index + 0] = (block[index + 0] *
  65. a->q_intra_matrix[index + 0] + (1 << 15)) >> 16))
  66. ccp |= 8;
  67. if ((block[index + 8] = (block[index + 8] *
  68. a->q_intra_matrix[index + 8] + (1 << 15)) >> 16))
  69. ccp |= 4;
  70. if ((block[index + 1] = (block[index + 1] *
  71. a->q_intra_matrix[index + 1] + (1 << 15)) >> 16))
  72. ccp |= 2;
  73. if ((block[index + 9] = (block[index + 9] *
  74. a->q_intra_matrix[index + 9] + (1 << 15)) >> 16))
  75. ccp |= 1;
  76. if (ccp) {
  77. for (; nc_count; nc_count--)
  78. put_bits(&a->pb, ff_asv_ccp_tab[0][1], ff_asv_ccp_tab[0][0]);
  79. put_bits(&a->pb, ff_asv_ccp_tab[ccp][1], ff_asv_ccp_tab[ccp][0]);
  80. if (ccp & 8)
  81. asv1_put_level(&a->pb, block[index + 0]);
  82. if (ccp & 4)
  83. asv1_put_level(&a->pb, block[index + 8]);
  84. if (ccp & 2)
  85. asv1_put_level(&a->pb, block[index + 1]);
  86. if (ccp & 1)
  87. asv1_put_level(&a->pb, block[index + 9]);
  88. } else {
  89. nc_count++;
  90. }
  91. }
  92. put_bits(&a->pb, ff_asv_ccp_tab[16][1], ff_asv_ccp_tab[16][0]);
  93. }
  94. static inline void asv2_encode_block(ASV1Context *a, int16_t block[64])
  95. {
  96. int i;
  97. int count = 0;
  98. for (count = 63; count > 3; count--) {
  99. const int index = ff_asv_scantab[count];
  100. if ((block[index] * a->q_intra_matrix[index] + (1 << 15)) >> 16)
  101. break;
  102. }
  103. count >>= 2;
  104. asv2_put_bits(&a->pb, 4, count);
  105. asv2_put_bits(&a->pb, 8, (block[0] + 32) >> 6);
  106. block[0] = 0;
  107. for (i = 0; i <= count; i++) {
  108. const int index = ff_asv_scantab[4 * i];
  109. int ccp = 0;
  110. if ((block[index + 0] = (block[index + 0] *
  111. a->q_intra_matrix[index + 0] + (1 << 15)) >> 16))
  112. ccp |= 8;
  113. if ((block[index + 8] = (block[index + 8] *
  114. a->q_intra_matrix[index + 8] + (1 << 15)) >> 16))
  115. ccp |= 4;
  116. if ((block[index + 1] = (block[index + 1] *
  117. a->q_intra_matrix[index + 1] + (1 << 15)) >> 16))
  118. ccp |= 2;
  119. if ((block[index + 9] = (block[index + 9] *
  120. a->q_intra_matrix[index + 9] + (1 << 15)) >> 16))
  121. ccp |= 1;
  122. assert(i || ccp < 8);
  123. if (i)
  124. put_bits(&a->pb, ff_asv_ac_ccp_tab[ccp][1], ff_asv_ac_ccp_tab[ccp][0]);
  125. else
  126. put_bits(&a->pb, ff_asv_dc_ccp_tab[ccp][1], ff_asv_dc_ccp_tab[ccp][0]);
  127. if (ccp) {
  128. if (ccp & 8)
  129. asv2_put_level(&a->pb, block[index + 0]);
  130. if (ccp & 4)
  131. asv2_put_level(&a->pb, block[index + 8]);
  132. if (ccp & 2)
  133. asv2_put_level(&a->pb, block[index + 1]);
  134. if (ccp & 1)
  135. asv2_put_level(&a->pb, block[index + 9]);
  136. }
  137. }
  138. }
  139. #define MAX_MB_SIZE (30 * 16 * 16 * 3 / 2 / 8)
  140. static inline int encode_mb(ASV1Context *a, int16_t block[6][64])
  141. {
  142. int i;
  143. if (a->pb.buf_end - a->pb.buf - (put_bits_count(&a->pb) >> 3) < MAX_MB_SIZE) {
  144. av_log(a->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  145. return -1;
  146. }
  147. if (a->avctx->codec_id == AV_CODEC_ID_ASV1) {
  148. for (i = 0; i < 6; i++)
  149. asv1_encode_block(a, block[i]);
  150. } else {
  151. for (i = 0; i < 6; i++)
  152. asv2_encode_block(a, block[i]);
  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 & 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 (!pkt->data &&
  185. (ret = av_new_packet(pkt, a->mb_height * a->mb_width * MAX_MB_SIZE +
  186. FF_MIN_BUFFER_SIZE)) < 0) {
  187. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  188. return ret;
  189. }
  190. init_put_bits(&a->pb, pkt->data, pkt->size);
  191. for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
  192. for (mb_x = 0; mb_x < a->mb_width2; mb_x++) {
  193. dct_get(a, pict, mb_x, mb_y);
  194. encode_mb(a, a->block);
  195. }
  196. }
  197. if (a->mb_width2 != a->mb_width) {
  198. mb_x = a->mb_width2;
  199. for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
  200. dct_get(a, pict, mb_x, mb_y);
  201. encode_mb(a, a->block);
  202. }
  203. }
  204. if (a->mb_height2 != a->mb_height) {
  205. mb_y = a->mb_height2;
  206. for (mb_x = 0; mb_x < a->mb_width; mb_x++) {
  207. dct_get(a, pict, mb_x, mb_y);
  208. encode_mb(a, a->block);
  209. }
  210. }
  211. emms_c();
  212. avpriv_align_put_bits(&a->pb);
  213. while (put_bits_count(&a->pb) & 31)
  214. put_bits(&a->pb, 8, 0);
  215. size = put_bits_count(&a->pb) / 32;
  216. if (avctx->codec_id == AV_CODEC_ID_ASV1) {
  217. a->bbdsp.bswap_buf((uint32_t *) pkt->data,
  218. (uint32_t *) pkt->data, size);
  219. } else {
  220. int i;
  221. for (i = 0; i < 4 * size; i++)
  222. pkt->data[i] = ff_reverse[pkt->data[i]];
  223. }
  224. pkt->size = size * 4;
  225. pkt->flags |= AV_PKT_FLAG_KEY;
  226. *got_packet = 1;
  227. return 0;
  228. }
  229. static av_cold int encode_init(AVCodecContext *avctx)
  230. {
  231. ASV1Context *const a = avctx->priv_data;
  232. int i;
  233. const int scale = avctx->codec_id == AV_CODEC_ID_ASV1 ? 1 : 2;
  234. avctx->coded_frame = av_frame_alloc();
  235. if (!avctx->coded_frame)
  236. return AVERROR(ENOMEM);
  237. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  238. avctx->coded_frame->key_frame = 1;
  239. ff_asv_common_init(avctx);
  240. ff_fdctdsp_init(&a->fdsp, avctx);
  241. ff_pixblockdsp_init(&a->pdsp, avctx);
  242. if (avctx->global_quality == 0)
  243. avctx->global_quality = 4 * FF_QUALITY_SCALE;
  244. a->inv_qscale = (32 * scale * FF_QUALITY_SCALE +
  245. avctx->global_quality / 2) / avctx->global_quality;
  246. avctx->extradata = av_mallocz(8);
  247. avctx->extradata_size = 8;
  248. ((uint32_t *) avctx->extradata)[0] = av_le2ne32(a->inv_qscale);
  249. ((uint32_t *) avctx->extradata)[1] = av_le2ne32(AV_RL32("ASUS"));
  250. for (i = 0; i < 64; i++) {
  251. int q = 32 * scale * ff_mpeg1_default_intra_matrix[i];
  252. a->q_intra_matrix[i] = ((a->inv_qscale << 16) + q / 2) / q;
  253. }
  254. return 0;
  255. }
  256. static av_cold int asv_encode_close(AVCodecContext *avctx)
  257. {
  258. av_frame_free(&avctx->coded_frame);
  259. return 0;
  260. }
  261. #if CONFIG_ASV1_ENCODER
  262. AVCodec ff_asv1_encoder = {
  263. .name = "asv1",
  264. .long_name = NULL_IF_CONFIG_SMALL("ASUS V1"),
  265. .type = AVMEDIA_TYPE_VIDEO,
  266. .id = AV_CODEC_ID_ASV1,
  267. .priv_data_size = sizeof(ASV1Context),
  268. .init = encode_init,
  269. .encode2 = encode_frame,
  270. .close = asv_encode_close,
  271. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  272. AV_PIX_FMT_NONE },
  273. };
  274. #endif
  275. #if CONFIG_ASV2_ENCODER
  276. AVCodec ff_asv2_encoder = {
  277. .name = "asv2",
  278. .long_name = NULL_IF_CONFIG_SMALL("ASUS V2"),
  279. .type = AVMEDIA_TYPE_VIDEO,
  280. .id = AV_CODEC_ID_ASV2,
  281. .priv_data_size = sizeof(ASV1Context),
  282. .init = encode_init,
  283. .encode2 = encode_frame,
  284. .close = asv_encode_close,
  285. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  286. AV_PIX_FMT_NONE },
  287. };
  288. #endif