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.

191 lines
5.8KB

  1. /*
  2. * Cirrus Logic AccuPak (CLJR) codec
  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 codec.
  24. */
  25. #include "avcodec.h"
  26. #include "libavutil/opt.h"
  27. #include "get_bits.h"
  28. #include "internal.h"
  29. #include "put_bits.h"
  30. #if CONFIG_CLJR_DECODER
  31. static int decode_frame(AVCodecContext *avctx,
  32. void *data, int *got_frame,
  33. AVPacket *avpkt)
  34. {
  35. const uint8_t *buf = avpkt->data;
  36. int buf_size = avpkt->size;
  37. GetBitContext gb;
  38. AVFrame * const p = data;
  39. int x, y, ret;
  40. if (avctx->height <= 0 || avctx->width <= 0) {
  41. av_log(avctx, AV_LOG_ERROR, "Invalid width or height\n");
  42. return AVERROR_INVALIDDATA;
  43. }
  44. if (buf_size / avctx->height < avctx->width) {
  45. av_log(avctx, AV_LOG_ERROR,
  46. "Resolution larger than buffer size. Invalid header?\n");
  47. return AVERROR_INVALIDDATA;
  48. }
  49. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  50. return ret;
  51. p->pict_type = AV_PICTURE_TYPE_I;
  52. p->key_frame = 1;
  53. init_get_bits(&gb, buf, buf_size * 8);
  54. for (y = 0; y < avctx->height; y++) {
  55. uint8_t *luma = &p->data[0][y * p->linesize[0]];
  56. uint8_t *cb = &p->data[1][y * p->linesize[1]];
  57. uint8_t *cr = &p->data[2][y * p->linesize[2]];
  58. for (x = 0; x < avctx->width; x += 4) {
  59. luma[3] = (get_bits(&gb, 5)*33) >> 2;
  60. luma[2] = (get_bits(&gb, 5)*33) >> 2;
  61. luma[1] = (get_bits(&gb, 5)*33) >> 2;
  62. luma[0] = (get_bits(&gb, 5)*33) >> 2;
  63. luma += 4;
  64. *(cb++) = get_bits(&gb, 6) << 2;
  65. *(cr++) = get_bits(&gb, 6) << 2;
  66. }
  67. }
  68. *got_frame = 1;
  69. return buf_size;
  70. }
  71. static av_cold int decode_init(AVCodecContext *avctx)
  72. {
  73. avctx->pix_fmt = AV_PIX_FMT_YUV411P;
  74. return 0;
  75. }
  76. AVCodec ff_cljr_decoder = {
  77. .name = "cljr",
  78. .type = AVMEDIA_TYPE_VIDEO,
  79. .id = AV_CODEC_ID_CLJR,
  80. .init = decode_init,
  81. .decode = decode_frame,
  82. .capabilities = CODEC_CAP_DR1,
  83. .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
  84. };
  85. #endif
  86. #if CONFIG_CLJR_ENCODER
  87. typedef struct CLJRContext {
  88. AVClass *avclass;
  89. AVFrame picture;
  90. int dither_type;
  91. } CLJRContext;
  92. static av_cold int encode_init(AVCodecContext *avctx)
  93. {
  94. CLJRContext * const a = avctx->priv_data;
  95. avctx->coded_frame = &a->picture;
  96. return 0;
  97. }
  98. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  99. const AVFrame *p, int *got_packet)
  100. {
  101. CLJRContext *a = avctx->priv_data;
  102. PutBitContext pb;
  103. int x, y, ret;
  104. uint32_t dither= avctx->frame_number;
  105. static const uint32_t ordered_dither[2][2] =
  106. {
  107. { 0x10400000, 0x104F0000 },
  108. { 0xCB2A0000, 0xCB250000 },
  109. };
  110. if ((ret = ff_alloc_packet2(avctx, pkt, 32*avctx->height*avctx->width/4)) < 0)
  111. return ret;
  112. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  113. avctx->coded_frame->key_frame = 1;
  114. init_put_bits(&pb, pkt->data, pkt->size);
  115. for (y = 0; y < avctx->height; y++) {
  116. uint8_t *luma = &p->data[0][y * p->linesize[0]];
  117. uint8_t *cb = &p->data[1][y * p->linesize[1]];
  118. uint8_t *cr = &p->data[2][y * p->linesize[2]];
  119. for (x = 0; x < avctx->width; x += 4) {
  120. switch (a->dither_type) {
  121. case 0: dither = 0x492A0000; break;
  122. case 1: dither = dither * 1664525 + 1013904223; break;
  123. case 2: dither = ordered_dither[ y&1 ][ (x>>2)&1 ];break;
  124. }
  125. put_bits(&pb, 5, (249*(luma[3] + (dither>>29) )) >> 11);
  126. put_bits(&pb, 5, (249*(luma[2] + ((dither>>26)&7))) >> 11);
  127. put_bits(&pb, 5, (249*(luma[1] + ((dither>>23)&7))) >> 11);
  128. put_bits(&pb, 5, (249*(luma[0] + ((dither>>20)&7))) >> 11);
  129. luma += 4;
  130. put_bits(&pb, 6, (253*(*(cb++) + ((dither>>18)&3))) >> 10);
  131. put_bits(&pb, 6, (253*(*(cr++) + ((dither>>16)&3))) >> 10);
  132. }
  133. }
  134. flush_put_bits(&pb);
  135. pkt->size = put_bits_count(&pb) / 8;
  136. pkt->flags |= AV_PKT_FLAG_KEY;
  137. *got_packet = 1;
  138. return 0;
  139. }
  140. #define OFFSET(x) offsetof(CLJRContext, x)
  141. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  142. static const AVOption options[] = {
  143. { "dither_type", "Dither type", OFFSET(dither_type), AV_OPT_TYPE_INT, { .i64=1 }, 0, 2, VE},
  144. { NULL },
  145. };
  146. static const AVClass cljr_class = {
  147. .class_name = "cljr encoder",
  148. .item_name = av_default_item_name,
  149. .option = options,
  150. .version = LIBAVUTIL_VERSION_INT,
  151. };
  152. AVCodec ff_cljr_encoder = {
  153. .name = "cljr",
  154. .type = AVMEDIA_TYPE_VIDEO,
  155. .id = AV_CODEC_ID_CLJR,
  156. .priv_data_size = sizeof(CLJRContext),
  157. .init = encode_init,
  158. .encode2 = encode_frame,
  159. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV411P,
  160. AV_PIX_FMT_NONE },
  161. .long_name = NULL_IF_CONFIG_SMALL("Cirrus Logic AccuPak"),
  162. .priv_class = &cljr_class,
  163. };
  164. #endif