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.

309 lines
9.2KB

  1. /*
  2. * LOCO codec
  3. * Copyright (c) 2005 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. * LOCO codec.
  24. */
  25. #include "avcodec.h"
  26. #include "get_bits.h"
  27. #include "golomb.h"
  28. #include "mathops.h"
  29. enum LOCO_MODE {LOCO_UNKN=0, LOCO_CYUY2=-1, LOCO_CRGB=-2, LOCO_CRGBA=-3, LOCO_CYV12=-4,
  30. LOCO_YUY2=1, LOCO_UYVY=2, LOCO_RGB=3, LOCO_RGBA=4, LOCO_YV12=5};
  31. typedef struct LOCOContext{
  32. AVCodecContext *avctx;
  33. AVFrame pic;
  34. int lossy;
  35. int mode;
  36. } LOCOContext;
  37. typedef struct RICEContext{
  38. GetBitContext gb;
  39. int save, run, run2; /* internal rice decoder state */
  40. int sum, count; /* sum and count for getting rice parameter */
  41. int lossy;
  42. }RICEContext;
  43. static int loco_get_rice_param(RICEContext *r)
  44. {
  45. int cnt = 0;
  46. int val = r->count;
  47. while(r->sum > val && cnt < 9) {
  48. val <<= 1;
  49. cnt++;
  50. }
  51. return cnt;
  52. }
  53. static inline void loco_update_rice_param(RICEContext *r, int val)
  54. {
  55. r->sum += val;
  56. r->count++;
  57. if(r->count == 16) {
  58. r->sum >>= 1;
  59. r->count >>= 1;
  60. }
  61. }
  62. static inline int loco_get_rice(RICEContext *r)
  63. {
  64. int v;
  65. if (r->run > 0) { /* we have zero run */
  66. r->run--;
  67. loco_update_rice_param(r, 0);
  68. return 0;
  69. }
  70. v = get_ur_golomb_jpegls(&r->gb, loco_get_rice_param(r), INT_MAX, 0);
  71. loco_update_rice_param(r, (v+1)>>1);
  72. if (!v) {
  73. if (r->save >= 0) {
  74. r->run = get_ur_golomb_jpegls(&r->gb, 2, INT_MAX, 0);
  75. if(r->run > 1)
  76. r->save += r->run + 1;
  77. else
  78. r->save -= 3;
  79. }
  80. else
  81. r->run2++;
  82. } else {
  83. v = ((v>>1) + r->lossy) ^ -(v&1);
  84. if (r->run2 > 0) {
  85. if (r->run2 > 2)
  86. r->save += r->run2;
  87. else
  88. r->save -= 3;
  89. r->run2 = 0;
  90. }
  91. }
  92. return v;
  93. }
  94. /* LOCO main predictor - LOCO-I/JPEG-LS predictor */
  95. static inline int loco_predict(uint8_t* data, int stride, int step)
  96. {
  97. int a, b, c;
  98. a = data[-stride];
  99. b = data[-step];
  100. c = data[-stride - step];
  101. return mid_pred(a, a + b - c, b);
  102. }
  103. static int loco_decode_plane(LOCOContext *l, uint8_t *data, int width, int height,
  104. int stride, const uint8_t *buf, int buf_size, int step)
  105. {
  106. RICEContext rc;
  107. int val;
  108. int i, j;
  109. if(buf_size<=0)
  110. return -1;
  111. init_get_bits(&rc.gb, buf, buf_size*8);
  112. rc.save = 0;
  113. rc.run = 0;
  114. rc.run2 = 0;
  115. rc.lossy = l->lossy;
  116. rc.sum = 8;
  117. rc.count = 1;
  118. /* restore top left pixel */
  119. val = loco_get_rice(&rc);
  120. data[0] = 128 + val;
  121. /* restore top line */
  122. for (i = 1; i < width; i++) {
  123. val = loco_get_rice(&rc);
  124. data[i * step] = data[i * step - step] + val;
  125. }
  126. data += stride;
  127. for (j = 1; j < height; j++) {
  128. /* restore left column */
  129. val = loco_get_rice(&rc);
  130. data[0] = data[-stride] + val;
  131. /* restore all other pixels */
  132. for (i = 1; i < width; i++) {
  133. val = loco_get_rice(&rc);
  134. data[i * step] = loco_predict(&data[i * step], stride, step) + val;
  135. }
  136. data += stride;
  137. }
  138. return (get_bits_count(&rc.gb) + 7) >> 3;
  139. }
  140. static int decode_frame(AVCodecContext *avctx,
  141. void *data, int *data_size,
  142. AVPacket *avpkt)
  143. {
  144. const uint8_t *buf = avpkt->data;
  145. int buf_size = avpkt->size;
  146. LOCOContext * const l = avctx->priv_data;
  147. AVFrame * const p = &l->pic;
  148. int decoded;
  149. if(p->data[0])
  150. avctx->release_buffer(avctx, p);
  151. p->reference = 0;
  152. if(avctx->get_buffer(avctx, p) < 0){
  153. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  154. return -1;
  155. }
  156. p->key_frame = 1;
  157. #define ADVANCE_BY_DECODED do { \
  158. if (decoded < 0) goto stop; \
  159. buf += decoded; buf_size -= decoded; \
  160. } while(0)
  161. switch(l->mode) {
  162. case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
  163. decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
  164. p->linesize[0], buf, buf_size, 1);
  165. ADVANCE_BY_DECODED;
  166. decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height,
  167. p->linesize[1], buf, buf_size, 1);
  168. ADVANCE_BY_DECODED;
  169. decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height,
  170. p->linesize[2], buf, buf_size, 1);
  171. break;
  172. case LOCO_CYV12: case LOCO_YV12:
  173. decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
  174. p->linesize[0], buf, buf_size, 1);
  175. ADVANCE_BY_DECODED;
  176. decoded = loco_decode_plane(l, p->data[2], avctx->width / 2, avctx->height / 2,
  177. p->linesize[2], buf, buf_size, 1);
  178. ADVANCE_BY_DECODED;
  179. decoded = loco_decode_plane(l, p->data[1], avctx->width / 2, avctx->height / 2,
  180. p->linesize[1], buf, buf_size, 1);
  181. break;
  182. case LOCO_CRGB: case LOCO_RGB:
  183. decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1), avctx->width, avctx->height,
  184. -p->linesize[0], buf, buf_size, 3);
  185. ADVANCE_BY_DECODED;
  186. decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1) + 1, avctx->width, avctx->height,
  187. -p->linesize[0], buf, buf_size, 3);
  188. ADVANCE_BY_DECODED;
  189. decoded = loco_decode_plane(l, p->data[0] + p->linesize[0]*(avctx->height-1) + 2, avctx->width, avctx->height,
  190. -p->linesize[0], buf, buf_size, 3);
  191. break;
  192. case LOCO_CRGBA: case LOCO_RGBA:
  193. decoded = loco_decode_plane(l, p->data[0], avctx->width, avctx->height,
  194. p->linesize[0], buf, buf_size, 4);
  195. ADVANCE_BY_DECODED;
  196. decoded = loco_decode_plane(l, p->data[0] + 1, avctx->width, avctx->height,
  197. p->linesize[0], buf, buf_size, 4);
  198. ADVANCE_BY_DECODED;
  199. decoded = loco_decode_plane(l, p->data[0] + 2, avctx->width, avctx->height,
  200. p->linesize[0], buf, buf_size, 4);
  201. ADVANCE_BY_DECODED;
  202. decoded = loco_decode_plane(l, p->data[0] + 3, avctx->width, avctx->height,
  203. p->linesize[0], buf, buf_size, 4);
  204. break;
  205. }
  206. stop:
  207. *data_size = sizeof(AVFrame);
  208. *(AVFrame*)data = l->pic;
  209. return buf_size < 0 ? -1 : avpkt->size - buf_size;
  210. }
  211. static av_cold int decode_init(AVCodecContext *avctx){
  212. LOCOContext * const l = avctx->priv_data;
  213. int version;
  214. l->avctx = avctx;
  215. if (avctx->extradata_size < 12) {
  216. av_log(avctx, AV_LOG_ERROR, "Extradata size must be >= 12 instead of %i\n",
  217. avctx->extradata_size);
  218. return -1;
  219. }
  220. version = AV_RL32(avctx->extradata);
  221. switch(version) {
  222. case 1:
  223. l->lossy = 0;
  224. break;
  225. case 2:
  226. l->lossy = AV_RL32(avctx->extradata + 8);
  227. break;
  228. default:
  229. l->lossy = AV_RL32(avctx->extradata + 8);
  230. av_log_ask_for_sample(avctx, "This is LOCO codec version %i.\n", version);
  231. }
  232. l->mode = AV_RL32(avctx->extradata + 4);
  233. switch(l->mode) {
  234. case LOCO_CYUY2: case LOCO_YUY2: case LOCO_UYVY:
  235. avctx->pix_fmt = PIX_FMT_YUV422P;
  236. break;
  237. case LOCO_CRGB: case LOCO_RGB:
  238. avctx->pix_fmt = PIX_FMT_BGR24;
  239. break;
  240. case LOCO_CYV12: case LOCO_YV12:
  241. avctx->pix_fmt = PIX_FMT_YUV420P;
  242. break;
  243. case LOCO_CRGBA: case LOCO_RGBA:
  244. avctx->pix_fmt = PIX_FMT_RGB32;
  245. break;
  246. default:
  247. av_log(avctx, AV_LOG_INFO, "Unknown colorspace, index = %i\n", l->mode);
  248. return -1;
  249. }
  250. if(avctx->debug & FF_DEBUG_PICT_INFO)
  251. av_log(avctx, AV_LOG_INFO, "lossy:%i, version:%i, mode: %i\n", l->lossy, version, l->mode);
  252. avcodec_get_frame_defaults(&l->pic);
  253. return 0;
  254. }
  255. static av_cold int decode_end(AVCodecContext *avctx){
  256. LOCOContext * const l = avctx->priv_data;
  257. AVFrame *pic = &l->pic;
  258. if (pic->data[0])
  259. avctx->release_buffer(avctx, pic);
  260. return 0;
  261. }
  262. AVCodec ff_loco_decoder = {
  263. .name = "loco",
  264. .type = AVMEDIA_TYPE_VIDEO,
  265. .id = AV_CODEC_ID_LOCO,
  266. .priv_data_size = sizeof(LOCOContext),
  267. .init = decode_init,
  268. .close = decode_end,
  269. .decode = decode_frame,
  270. .capabilities = CODEC_CAP_DR1,
  271. .long_name = NULL_IF_CONFIG_SMALL("LOCO"),
  272. };