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.

212 lines
6.4KB

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