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.

193 lines
5.9KB

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