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.

349 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 int 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. if (!i && ccp >= 8)
  123. return AVERROR_BUG;
  124. if (i)
  125. put_bits(&a->pb, ff_asv_ac_ccp_tab[ccp][1], ff_asv_ac_ccp_tab[ccp][0]);
  126. else
  127. put_bits(&a->pb, ff_asv_dc_ccp_tab[ccp][1], ff_asv_dc_ccp_tab[ccp][0]);
  128. if (ccp) {
  129. if (ccp & 8)
  130. asv2_put_level(&a->pb, block[index + 0]);
  131. if (ccp & 4)
  132. asv2_put_level(&a->pb, block[index + 8]);
  133. if (ccp & 2)
  134. asv2_put_level(&a->pb, block[index + 1]);
  135. if (ccp & 1)
  136. asv2_put_level(&a->pb, block[index + 9]);
  137. }
  138. }
  139. return 0;
  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, ret;
  145. if (a->pb.buf_end - a->pb.buf - (put_bits_count(&a->pb) >> 3) < MAX_MB_SIZE) {
  146. av_log(a->avctx, AV_LOG_ERROR, "encoded frame too large\n");
  147. return -1;
  148. }
  149. if (a->avctx->codec_id == AV_CODEC_ID_ASV1) {
  150. for (i = 0; i < 6; i++)
  151. asv1_encode_block(a, block[i]);
  152. } else {
  153. for (i = 0; i < 6; i++) {
  154. ret = asv2_encode_block(a, block[i]);
  155. if (ret < 0)
  156. return ret;
  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 & 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 (!pkt->data &&
  190. (ret = av_new_packet(pkt, a->mb_height * a->mb_width * MAX_MB_SIZE +
  191. FF_MIN_BUFFER_SIZE)) < 0) {
  192. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  193. return ret;
  194. }
  195. init_put_bits(&a->pb, pkt->data, pkt->size);
  196. for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
  197. for (mb_x = 0; mb_x < a->mb_width2; mb_x++) {
  198. dct_get(a, pict, mb_x, mb_y);
  199. encode_mb(a, a->block);
  200. }
  201. }
  202. if (a->mb_width2 != a->mb_width) {
  203. mb_x = a->mb_width2;
  204. for (mb_y = 0; mb_y < a->mb_height2; mb_y++) {
  205. dct_get(a, pict, mb_x, mb_y);
  206. encode_mb(a, a->block);
  207. }
  208. }
  209. if (a->mb_height2 != a->mb_height) {
  210. mb_y = a->mb_height2;
  211. for (mb_x = 0; mb_x < a->mb_width; mb_x++) {
  212. dct_get(a, pict, mb_x, mb_y);
  213. encode_mb(a, a->block);
  214. }
  215. }
  216. emms_c();
  217. avpriv_align_put_bits(&a->pb);
  218. while (put_bits_count(&a->pb) & 31)
  219. put_bits(&a->pb, 8, 0);
  220. size = put_bits_count(&a->pb) / 32;
  221. if (avctx->codec_id == AV_CODEC_ID_ASV1) {
  222. a->bbdsp.bswap_buf((uint32_t *) pkt->data,
  223. (uint32_t *) pkt->data, size);
  224. } else {
  225. int i;
  226. for (i = 0; i < 4 * size; i++)
  227. pkt->data[i] = ff_reverse[pkt->data[i]];
  228. }
  229. pkt->size = size * 4;
  230. pkt->flags |= AV_PKT_FLAG_KEY;
  231. *got_packet = 1;
  232. return 0;
  233. }
  234. static av_cold int encode_init(AVCodecContext *avctx)
  235. {
  236. ASV1Context *const a = avctx->priv_data;
  237. int i;
  238. const int scale = avctx->codec_id == AV_CODEC_ID_ASV1 ? 1 : 2;
  239. avctx->coded_frame = av_frame_alloc();
  240. if (!avctx->coded_frame)
  241. return AVERROR(ENOMEM);
  242. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  243. avctx->coded_frame->key_frame = 1;
  244. ff_asv_common_init(avctx);
  245. ff_fdctdsp_init(&a->fdsp, avctx);
  246. ff_pixblockdsp_init(&a->pdsp, avctx);
  247. if (avctx->global_quality == 0)
  248. avctx->global_quality = 4 * FF_QUALITY_SCALE;
  249. a->inv_qscale = (32 * scale * FF_QUALITY_SCALE +
  250. avctx->global_quality / 2) / avctx->global_quality;
  251. avctx->extradata = av_mallocz(8);
  252. avctx->extradata_size = 8;
  253. ((uint32_t *) avctx->extradata)[0] = av_le2ne32(a->inv_qscale);
  254. ((uint32_t *) avctx->extradata)[1] = av_le2ne32(AV_RL32("ASUS"));
  255. for (i = 0; i < 64; i++) {
  256. int q = 32 * scale * ff_mpeg1_default_intra_matrix[i];
  257. a->q_intra_matrix[i] = ((a->inv_qscale << 16) + q / 2) / q;
  258. }
  259. return 0;
  260. }
  261. static av_cold int asv_encode_close(AVCodecContext *avctx)
  262. {
  263. av_frame_free(&avctx->coded_frame);
  264. return 0;
  265. }
  266. #if CONFIG_ASV1_ENCODER
  267. AVCodec ff_asv1_encoder = {
  268. .name = "asv1",
  269. .long_name = NULL_IF_CONFIG_SMALL("ASUS V1"),
  270. .type = AVMEDIA_TYPE_VIDEO,
  271. .id = AV_CODEC_ID_ASV1,
  272. .priv_data_size = sizeof(ASV1Context),
  273. .init = encode_init,
  274. .encode2 = encode_frame,
  275. .close = asv_encode_close,
  276. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  277. AV_PIX_FMT_NONE },
  278. };
  279. #endif
  280. #if CONFIG_ASV2_ENCODER
  281. AVCodec ff_asv2_encoder = {
  282. .name = "asv2",
  283. .long_name = NULL_IF_CONFIG_SMALL("ASUS V2"),
  284. .type = AVMEDIA_TYPE_VIDEO,
  285. .id = AV_CODEC_ID_ASV2,
  286. .priv_data_size = sizeof(ASV1Context),
  287. .init = encode_init,
  288. .encode2 = encode_frame,
  289. .close = asv_encode_close,
  290. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
  291. AV_PIX_FMT_NONE },
  292. };
  293. #endif