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.

123 lines
4.2KB

  1. /*
  2. * Cirrus Logic AccuPak (CLJR) encoder
  3. * Copyright (c) 2003 Alex Beregszaszi
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Cirrus Logic AccuPak encoder.
  24. */
  25. #include "libavutil/common.h"
  26. #include "libavutil/opt.h"
  27. #include "avcodec.h"
  28. #include "internal.h"
  29. #include "put_bits.h"
  30. typedef struct CLJRContext {
  31. AVClass *avclass;
  32. int dither_type;
  33. } CLJRContext;
  34. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  35. const AVFrame *p, int *got_packet)
  36. {
  37. CLJRContext *a = avctx->priv_data;
  38. PutBitContext pb;
  39. int x, y, ret;
  40. uint32_t dither= avctx->frame_number;
  41. static const uint32_t ordered_dither[2][2] =
  42. {
  43. { 0x10400000, 0x104F0000 },
  44. { 0xCB2A0000, 0xCB250000 },
  45. };
  46. if (avctx->width%4 && avctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) {
  47. av_log(avctx, AV_LOG_ERROR,
  48. "Widths which are not a multiple of 4 might fail with some decoders, "
  49. "use vstrict=-1 / -strict -1 to use %d anyway.\n", avctx->width);
  50. return AVERROR_EXPERIMENTAL;
  51. }
  52. if ((ret = ff_alloc_packet2(avctx, pkt, 32*avctx->height*avctx->width/4)) < 0)
  53. return ret;
  54. init_put_bits(&pb, pkt->data, pkt->size);
  55. for (y = 0; y < avctx->height; y++) {
  56. uint8_t *luma = &p->data[0][y * p->linesize[0]];
  57. uint8_t *cb = &p->data[1][y * p->linesize[1]];
  58. uint8_t *cr = &p->data[2][y * p->linesize[2]];
  59. uint8_t luma_tmp[4];
  60. for (x = 0; x < avctx->width; x += 4) {
  61. switch (a->dither_type) {
  62. case 0: dither = 0x492A0000; break;
  63. case 1: dither = dither * 1664525 + 1013904223; break;
  64. case 2: dither = ordered_dither[ y&1 ][ (x>>2)&1 ];break;
  65. }
  66. if (x+3 >= avctx->width) {
  67. memset(luma_tmp, 0, sizeof(luma_tmp));
  68. memcpy(luma_tmp, luma, avctx->width - x);
  69. luma = luma_tmp;
  70. }
  71. put_bits(&pb, 5, (249*(luma[3] + (dither>>29) )) >> 11);
  72. put_bits(&pb, 5, (249*(luma[2] + ((dither>>26)&7))) >> 11);
  73. put_bits(&pb, 5, (249*(luma[1] + ((dither>>23)&7))) >> 11);
  74. put_bits(&pb, 5, (249*(luma[0] + ((dither>>20)&7))) >> 11);
  75. luma += 4;
  76. put_bits(&pb, 6, (253*(*(cb++) + ((dither>>18)&3))) >> 10);
  77. put_bits(&pb, 6, (253*(*(cr++) + ((dither>>16)&3))) >> 10);
  78. }
  79. }
  80. flush_put_bits(&pb);
  81. pkt->size = put_bits_count(&pb) / 8;
  82. pkt->flags |= AV_PKT_FLAG_KEY;
  83. *got_packet = 1;
  84. return 0;
  85. }
  86. #define OFFSET(x) offsetof(CLJRContext, x)
  87. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  88. static const AVOption options[] = {
  89. { "dither_type", "Dither type", OFFSET(dither_type), AV_OPT_TYPE_INT, { .i64=1 }, 0, 2, VE},
  90. { NULL },
  91. };
  92. static const AVClass cljr_class = {
  93. .class_name = "cljr encoder",
  94. .item_name = av_default_item_name,
  95. .option = options,
  96. .version = LIBAVUTIL_VERSION_INT,
  97. };
  98. AVCodec ff_cljr_encoder = {
  99. .name = "cljr",
  100. .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
  101. .type = AVMEDIA_TYPE_VIDEO,
  102. .id = AV_CODEC_ID_CLJR,
  103. .priv_data_size = sizeof(CLJRContext),
  104. .encode2 = encode_frame,
  105. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV411P,
  106. AV_PIX_FMT_NONE },
  107. .priv_class = &cljr_class,
  108. };