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.

303 lines
7.0KB

  1. /*
  2. * QPEG codec
  3. * Copyright (c) 2004 Konstantin Shishkov
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file qpeg.c
  22. * QPEG codec.
  23. */
  24. #include "avcodec.h"
  25. #include "mpegvideo.h"
  26. typedef struct QpegContext{
  27. AVCodecContext *avctx;
  28. AVFrame pic;
  29. uint8_t *refdata;
  30. } QpegContext;
  31. static void qpeg_decode_intra(uint8_t *src, uint8_t *dst, int size,
  32. int stride, int width, int height)
  33. {
  34. int i;
  35. int code;
  36. int c0, c1;
  37. int run, copy;
  38. int filled = 0;
  39. height--;
  40. dst = dst + height * stride;
  41. while(size > 0) {
  42. code = *src++;
  43. size--;
  44. run = copy = 0;
  45. if(code == 0xFC) /* end-of-picture code */
  46. break;
  47. if(code >= 0xF8) { /* very long run */
  48. c0 = *src++;
  49. c1 = *src++;
  50. size -= 2;
  51. run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
  52. } else if (code >= 0xF0) { /* long run */
  53. c0 = *src++;
  54. size--;
  55. run = ((code & 0xF) << 8) + c0 + 2;
  56. } else if (code >= 0xE0) { /* short run */
  57. run = (code & 0x1F) + 2;
  58. } else if (code >= 0xC0) { /* very long copy */
  59. c0 = *src++;
  60. c1 = *src++;
  61. size -= 2;
  62. copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
  63. } else if (code >= 0x80) { /* long copy */
  64. c0 = *src++;
  65. size--;
  66. copy = ((code & 0x7F) << 8) + c0 + 1;
  67. } else { /* short copy */
  68. copy = code + 1;
  69. }
  70. /* perform actual run or copy */
  71. if(run) {
  72. int p;
  73. p = *src++;
  74. size--;
  75. for(i = 0; i < run; i++) {
  76. dst[filled++] = p;
  77. if (filled >= width) {
  78. filled = 0;
  79. dst -= stride;
  80. }
  81. }
  82. } else {
  83. for(i = 0; i < copy; i++) {
  84. dst[filled++] = *src++;
  85. if (filled >= width) {
  86. filled = 0;
  87. dst -= stride;
  88. }
  89. }
  90. size -= copy;
  91. }
  92. }
  93. }
  94. static int qpeg_table_h[16] =
  95. { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
  96. static int qpeg_table_w[16] =
  97. { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
  98. /* Decodes delta frames */
  99. static void qpeg_decode_inter(uint8_t *src, uint8_t *dst, int size,
  100. int stride, int width, int height,
  101. int delta, uint8_t *ctable, uint8_t *refdata)
  102. {
  103. int i, j;
  104. int code;
  105. int filled = 0;
  106. uint8_t *blkdata;
  107. /* copy prev frame */
  108. for(i = 0; i < height; i++)
  109. memcpy(refdata + (i * width), dst + (i * stride), width);
  110. blkdata = src - 0x86;
  111. height--;
  112. dst = dst + height * stride;
  113. while(size > 0) {
  114. code = *src++;
  115. size--;
  116. if(delta) {
  117. /* motion compensation */
  118. while((code & 0xF0) == 0xF0) {
  119. if(delta == 1) {
  120. int me_idx;
  121. int me_w, me_h, me_x, me_y;
  122. uint8_t *me_plane;
  123. int corr, val;
  124. /* get block size by index */
  125. me_idx = code & 0xF;
  126. me_w = qpeg_table_w[me_idx];
  127. me_h = qpeg_table_h[me_idx];
  128. /* extract motion vector */
  129. corr = *src++;
  130. size--;
  131. val = corr >> 4;
  132. if(val > 7)
  133. val -= 16;
  134. me_x = val;
  135. val = corr & 0xF;
  136. if(val > 7)
  137. val -= 16;
  138. me_y = val;
  139. /* do motion compensation */
  140. me_plane = refdata + (filled + me_x) + (height - me_y) * width;
  141. for(j = 0; j < me_h; j++) {
  142. for(i = 0; i < me_w; i++)
  143. dst[filled + i - (j * stride)] = me_plane[i - (j * width)];
  144. }
  145. }
  146. code = *src++;
  147. size--;
  148. }
  149. }
  150. if(code == 0xE0) /* end-of-picture code */
  151. break;
  152. if(code > 0xE0) { /* run code: 0xE1..0xFF */
  153. int p;
  154. code &= 0x1F;
  155. p = *src++;
  156. size--;
  157. for(i = 0; i <= code; i++) {
  158. dst[filled++] = p;
  159. if(filled >= width) {
  160. filled = 0;
  161. dst -= stride;
  162. height--;
  163. }
  164. }
  165. } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
  166. code &= 0x1F;
  167. for(i = 0; i <= code; i++) {
  168. dst[filled++] = *src++;
  169. if(filled >= width) {
  170. filled = 0;
  171. dst -= stride;
  172. height--;
  173. }
  174. }
  175. size -= code + 1;
  176. } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
  177. int skip;
  178. code &= 0x3F;
  179. /* codes 0x80 and 0x81 are actually escape codes,
  180. skip value minus constant is in the next byte */
  181. if(!code)
  182. skip = (*src++) + 64;
  183. else if(code == 1)
  184. skip = (*src++) + 320;
  185. else
  186. skip = code;
  187. filled += skip;
  188. while( filled >= width) {
  189. filled -= width;
  190. dst -= stride;
  191. height--;
  192. }
  193. } else {
  194. /* zero code treated as one-pixel skip */
  195. if(code)
  196. dst[filled++] = ctable[code & 0x7F];
  197. else
  198. filled++;
  199. if(filled >= width) {
  200. filled = 0;
  201. dst -= stride;
  202. height--;
  203. }
  204. }
  205. }
  206. }
  207. static int decode_frame(AVCodecContext *avctx,
  208. void *data, int *data_size,
  209. uint8_t *buf, int buf_size)
  210. {
  211. QpegContext * const a = avctx->priv_data;
  212. AVFrame * const p= (AVFrame*)&a->pic;
  213. uint8_t* outdata;
  214. int delta;
  215. if(p->data[0])
  216. avctx->release_buffer(avctx, p);
  217. p->reference= 0;
  218. if(avctx->get_buffer(avctx, p) < 0){
  219. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  220. return -1;
  221. }
  222. outdata = a->pic.data[0];
  223. if(buf[0x85] == 0x10) {
  224. qpeg_decode_intra(buf+0x86, outdata, buf_size - 0x86, a->pic.linesize[0], avctx->width, avctx->height);
  225. } else {
  226. delta = buf[0x85];
  227. qpeg_decode_inter(buf+0x86, outdata, buf_size - 0x86, a->pic.linesize[0], avctx->width, avctx->height, delta, buf + 4, a->refdata);
  228. }
  229. /* make the palette available on the way out */
  230. memcpy(a->pic.data[1], a->avctx->palctrl->palette, AVPALETTE_SIZE);
  231. if (a->avctx->palctrl->palette_changed) {
  232. a->pic.palette_has_changed = 1;
  233. a->avctx->palctrl->palette_changed = 0;
  234. }
  235. *data_size = sizeof(AVFrame);
  236. *(AVFrame*)data = a->pic;
  237. return buf_size;
  238. }
  239. static int decode_init(AVCodecContext *avctx){
  240. QpegContext * const a = avctx->priv_data;
  241. a->avctx = avctx;
  242. avctx->pix_fmt= PIX_FMT_PAL8;
  243. avctx->has_b_frames = 0;
  244. a->pic.data[0] = NULL;
  245. a->refdata = av_malloc(avctx->width * avctx->height);
  246. return 0;
  247. }
  248. static int decode_end(AVCodecContext *avctx){
  249. QpegContext * const a = avctx->priv_data;
  250. AVFrame * const p= (AVFrame*)&a->pic;
  251. if(p->data[0])
  252. avctx->release_buffer(avctx, p);
  253. av_free(a->refdata);
  254. return 0;
  255. }
  256. AVCodec qpeg_decoder = {
  257. "qpeg",
  258. CODEC_TYPE_VIDEO,
  259. CODEC_ID_QPEG,
  260. sizeof(QpegContext),
  261. decode_init,
  262. NULL,
  263. decode_end,
  264. decode_frame,
  265. CODEC_CAP_DR1,
  266. };