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.

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