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.

313 lines
10KB

  1. /*
  2. * Microsoft Video-1 Encoder
  3. * Copyright (c) 2009 Konstantin Shishkov
  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. * Microsoft Video-1 encoder
  24. */
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. #include "libavutil/lfg.h"
  28. #include "elbg.h"
  29. #include "libavutil/imgutils.h"
  30. /**
  31. * Encoder context
  32. */
  33. typedef struct Msvideo1EncContext {
  34. AVCodecContext *avctx;
  35. AVFrame pic;
  36. AVLFG rnd;
  37. uint8_t *prev;
  38. int block[16*3];
  39. int block2[16*3];
  40. int codebook[8*3];
  41. int codebook2[8*3];
  42. int output[16*3];
  43. int output2[16*3];
  44. int avg[3];
  45. int bestpos;
  46. int keyint;
  47. } Msvideo1EncContext;
  48. enum MSV1Mode{
  49. MODE_SKIP = 0,
  50. MODE_FILL,
  51. MODE_2COL,
  52. MODE_8COL,
  53. };
  54. #define SKIP_PREFIX 0x8400
  55. #define SKIPS_MAX 0x0FFF
  56. #define MKRGB555(in, off) ((in[off] << 10) | (in[off + 1] << 5) | (in[off + 2]))
  57. static const int remap[16] = { 0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15 };
  58. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  59. const AVFrame *pict, int *got_packet)
  60. {
  61. Msvideo1EncContext * const c = avctx->priv_data;
  62. AVFrame * const p = &c->pic;
  63. uint16_t *src;
  64. uint8_t *prevptr;
  65. uint8_t *dst, *buf;
  66. int keyframe = 1;
  67. int no_skips = 1;
  68. int i, j, k, x, y, ret;
  69. int skips = 0;
  70. if (!pkt->data &&
  71. (ret = av_new_packet(pkt, avctx->width*avctx->height*9 + FF_MIN_BUFFER_SIZE)) < 0) {
  72. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  73. return ret;
  74. }
  75. dst= buf= pkt->data;
  76. *p = *pict;
  77. if(!c->prev)
  78. c->prev = av_malloc(avctx->width * 3 * (avctx->height + 3));
  79. prevptr = c->prev + avctx->width * 3 * (FFALIGN(avctx->height, 4) - 1);
  80. src = (uint16_t*)(p->data[0] + p->linesize[0]*(FFALIGN(avctx->height, 4) - 1));
  81. if(c->keyint >= avctx->keyint_min)
  82. keyframe = 1;
  83. p->quality = 24;
  84. for(y = 0; y < avctx->height; y += 4){
  85. for(x = 0; x < avctx->width; x += 4){
  86. int bestmode = MODE_SKIP;
  87. int bestscore = INT_MAX;
  88. int flags = 0;
  89. int score;
  90. for(j = 0; j < 4; j++){
  91. for(i = 0; i < 4; i++){
  92. uint16_t val = src[x + i - j*p->linesize[0]/2];
  93. for(k = 0; k < 3; k++){
  94. c->block[(i + j*4)*3 + k] =
  95. c->block2[remap[i + j*4]*3 + k] = (val >> (10-k*5)) & 0x1F;
  96. }
  97. }
  98. }
  99. if(!keyframe){
  100. bestscore = 0;
  101. for(j = 0; j < 4; j++){
  102. for(i = 0; i < 4*3; i++){
  103. int t = prevptr[x*3 + i + j*p->linesize[0]] - c->block[i + j*4*3];
  104. bestscore += t*t;
  105. }
  106. }
  107. bestscore /= p->quality;
  108. }
  109. // try to find optimal value to fill whole 4x4 block
  110. score = 0;
  111. ff_init_elbg(c->block, 3, 16, c->avg, 1, 1, c->output, &c->rnd);
  112. ff_do_elbg (c->block, 3, 16, c->avg, 1, 1, c->output, &c->rnd);
  113. if(c->avg[0] == 1) // red component = 1 will be written as skip code
  114. c->avg[0] = 0;
  115. for(j = 0; j < 4; j++){
  116. for(i = 0; i < 4; i++){
  117. for(k = 0; k < 3; k++){
  118. int t = c->avg[k] - c->block[(i+j*4)*3+k];
  119. score += t*t;
  120. }
  121. }
  122. }
  123. score /= p->quality;
  124. score += 2;
  125. if(score < bestscore){
  126. bestscore = score;
  127. bestmode = MODE_FILL;
  128. }
  129. // search for optimal filling of 2-color block
  130. score = 0;
  131. ff_init_elbg(c->block, 3, 16, c->codebook, 2, 1, c->output, &c->rnd);
  132. ff_do_elbg (c->block, 3, 16, c->codebook, 2, 1, c->output, &c->rnd);
  133. // last output value should be always 1, swap codebooks if needed
  134. if(!c->output[15]){
  135. for(i = 0; i < 3; i++)
  136. FFSWAP(uint8_t, c->codebook[i], c->codebook[i+3]);
  137. for(i = 0; i < 16; i++)
  138. c->output[i] ^= 1;
  139. }
  140. for(j = 0; j < 4; j++){
  141. for(i = 0; i < 4; i++){
  142. for(k = 0; k < 3; k++){
  143. int t = c->codebook[c->output[i+j*4]*3 + k] - c->block[i*3+k+j*4*3];
  144. score += t*t;
  145. }
  146. }
  147. }
  148. score /= p->quality;
  149. score += 6;
  150. if(score < bestscore){
  151. bestscore = score;
  152. bestmode = MODE_2COL;
  153. }
  154. // search for optimal filling of 2-color 2x2 subblocks
  155. score = 0;
  156. for(i = 0; i < 4; i++){
  157. ff_init_elbg(c->block2 + i*4*3, 3, 4, c->codebook2 + i*2*3, 2, 1, c->output2 + i*4, &c->rnd);
  158. ff_do_elbg (c->block2 + i*4*3, 3, 4, c->codebook2 + i*2*3, 2, 1, c->output2 + i*4, &c->rnd);
  159. }
  160. // last value should be always 1, swap codebooks if needed
  161. if(!c->output2[15]){
  162. for(i = 0; i < 3; i++)
  163. FFSWAP(uint8_t, c->codebook2[i+18], c->codebook2[i+21]);
  164. for(i = 12; i < 16; i++)
  165. c->output2[i] ^= 1;
  166. }
  167. for(j = 0; j < 4; j++){
  168. for(i = 0; i < 4; i++){
  169. for(k = 0; k < 3; k++){
  170. int t = c->codebook2[(c->output2[remap[i+j*4]] + (i&2) + (j&2)*2)*3+k] - c->block[i*3+k + j*4*3];
  171. score += t*t;
  172. }
  173. }
  174. }
  175. score /= p->quality;
  176. score += 18;
  177. if(score < bestscore){
  178. bestscore = score;
  179. bestmode = MODE_8COL;
  180. }
  181. if(bestmode == MODE_SKIP){
  182. skips++;
  183. no_skips = 0;
  184. }
  185. if((bestmode != MODE_SKIP && skips) || skips == SKIPS_MAX){
  186. bytestream_put_le16(&dst, skips | SKIP_PREFIX);
  187. skips = 0;
  188. }
  189. switch(bestmode){
  190. case MODE_FILL:
  191. bytestream_put_le16(&dst, MKRGB555(c->avg,0) | 0x8000);
  192. for(j = 0; j < 4; j++)
  193. for(i = 0; i < 4; i++)
  194. for(k = 0; k < 3; k++)
  195. prevptr[i*3 + k - j*3*avctx->width] = c->avg[k];
  196. break;
  197. case MODE_2COL:
  198. for(j = 0; j < 4; j++){
  199. for(i = 0; i < 4; i++){
  200. flags |= (c->output[i + j*4]^1) << (i + j*4);
  201. for(k = 0; k < 3; k++)
  202. prevptr[i*3 + k - j*3*avctx->width] = c->codebook[c->output[i + j*4]*3 + k];
  203. }
  204. }
  205. bytestream_put_le16(&dst, flags);
  206. bytestream_put_le16(&dst, MKRGB555(c->codebook, 0));
  207. bytestream_put_le16(&dst, MKRGB555(c->codebook, 3));
  208. break;
  209. case MODE_8COL:
  210. for(j = 0; j < 4; j++){
  211. for(i = 0; i < 4; i++){
  212. flags |= (c->output2[remap[i + j*4]]^1) << (i + j*4);
  213. for(k = 0; k < 3; k++)
  214. prevptr[i*3 + k - j*3*avctx->width] = c->codebook2[(c->output2[remap[i+j*4]] + (i&2) + (j&2)*2)*3 + k];
  215. }
  216. }
  217. bytestream_put_le16(&dst, flags);
  218. bytestream_put_le16(&dst, MKRGB555(c->codebook2, 0) | 0x8000);
  219. for(i = 3; i < 24; i += 3)
  220. bytestream_put_le16(&dst, MKRGB555(c->codebook2, i));
  221. break;
  222. }
  223. }
  224. src -= p->linesize[0] << 1;
  225. prevptr -= avctx->width * 3 * 4;
  226. }
  227. if(skips)
  228. bytestream_put_le16(&dst, skips | SKIP_PREFIX);
  229. //EOF
  230. bytestream_put_byte(&dst, 0);
  231. bytestream_put_byte(&dst, 0);
  232. if(no_skips)
  233. keyframe = 1;
  234. if(keyframe)
  235. c->keyint = 0;
  236. else
  237. c->keyint++;
  238. p->pict_type= keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  239. p->key_frame= keyframe;
  240. if (keyframe) pkt->flags |= AV_PKT_FLAG_KEY;
  241. pkt->size = dst - buf;
  242. *got_packet = 1;
  243. return 0;
  244. }
  245. /**
  246. * init encoder
  247. */
  248. static av_cold int encode_init(AVCodecContext *avctx)
  249. {
  250. Msvideo1EncContext * const c = avctx->priv_data;
  251. c->avctx = avctx;
  252. if (av_image_check_size(avctx->width, avctx->height, 0, avctx) < 0) {
  253. return -1;
  254. }
  255. if((avctx->width&3) || (avctx->height&3)){
  256. av_log(avctx, AV_LOG_ERROR, "width and height must be multiplies of 4\n");
  257. return -1;
  258. }
  259. avcodec_get_frame_defaults(&c->pic);
  260. avctx->coded_frame = (AVFrame*)&c->pic;
  261. c->keyint = avctx->keyint_min;
  262. av_lfg_init(&c->rnd, 1);
  263. return 0;
  264. }
  265. /**
  266. * Uninit encoder
  267. */
  268. static av_cold int encode_end(AVCodecContext *avctx)
  269. {
  270. Msvideo1EncContext * const c = avctx->priv_data;
  271. av_freep(&c->prev);
  272. return 0;
  273. }
  274. AVCodec ff_msvideo1_encoder = {
  275. .name = "msvideo1",
  276. .type = AVMEDIA_TYPE_VIDEO,
  277. .id = CODEC_ID_MSVIDEO1,
  278. .priv_data_size = sizeof(Msvideo1EncContext),
  279. .init = encode_init,
  280. .encode2 = encode_frame,
  281. .close = encode_end,
  282. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB555, PIX_FMT_NONE},
  283. .long_name = NULL_IF_CONFIG_SMALL("Microsoft Video-1"),
  284. };