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.

310 lines
9.4KB

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