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.

343 lines
10KB

  1. /*
  2. * QPEG codec
  3. * Copyright (c) 2004 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. * QPEG codec.
  24. */
  25. #include "avcodec.h"
  26. typedef struct QpegContext{
  27. AVCodecContext *avctx;
  28. AVFrame pic, ref;
  29. uint32_t pal[256];
  30. } QpegContext;
  31. static int qpeg_decode_intra(const 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. int rows_to_go;
  40. rows_to_go = height;
  41. height--;
  42. dst = dst + height * stride;
  43. while((size > 0) && (rows_to_go > 0)) {
  44. code = *src++;
  45. size--;
  46. run = copy = 0;
  47. if(code == 0xFC) /* end-of-picture code */
  48. break;
  49. if(code >= 0xF8) { /* very long run */
  50. c0 = *src++;
  51. c1 = *src++;
  52. size -= 2;
  53. run = ((code & 0x7) << 16) + (c0 << 8) + c1 + 2;
  54. } else if (code >= 0xF0) { /* long run */
  55. c0 = *src++;
  56. size--;
  57. run = ((code & 0xF) << 8) + c0 + 2;
  58. } else if (code >= 0xE0) { /* short run */
  59. run = (code & 0x1F) + 2;
  60. } else if (code >= 0xC0) { /* very long copy */
  61. c0 = *src++;
  62. c1 = *src++;
  63. size -= 2;
  64. copy = ((code & 0x3F) << 16) + (c0 << 8) + c1 + 1;
  65. } else if (code >= 0x80) { /* long copy */
  66. c0 = *src++;
  67. size--;
  68. copy = ((code & 0x7F) << 8) + c0 + 1;
  69. } else { /* short copy */
  70. copy = code + 1;
  71. }
  72. /* perform actual run or copy */
  73. if(run) {
  74. int p;
  75. p = *src++;
  76. size--;
  77. for(i = 0; i < run; i++) {
  78. dst[filled++] = p;
  79. if (filled >= width) {
  80. filled = 0;
  81. dst -= stride;
  82. rows_to_go--;
  83. if(rows_to_go <= 0)
  84. break;
  85. }
  86. }
  87. } else {
  88. size -= copy;
  89. if (size<0)
  90. return AVERROR_INVALIDDATA;
  91. for(i = 0; i < copy; i++) {
  92. dst[filled++] = *src++;
  93. if (filled >= width) {
  94. filled = 0;
  95. dst -= stride;
  96. rows_to_go--;
  97. if(rows_to_go <= 0)
  98. break;
  99. }
  100. }
  101. }
  102. }
  103. return 0;
  104. }
  105. static const int qpeg_table_h[16] =
  106. { 0x00, 0x20, 0x20, 0x20, 0x18, 0x10, 0x10, 0x20, 0x10, 0x08, 0x18, 0x08, 0x08, 0x18, 0x10, 0x04};
  107. static const int qpeg_table_w[16] =
  108. { 0x00, 0x20, 0x18, 0x08, 0x18, 0x10, 0x20, 0x10, 0x08, 0x10, 0x20, 0x20, 0x08, 0x10, 0x18, 0x04};
  109. /* Decodes delta frames */
  110. static void qpeg_decode_inter(const uint8_t *src, uint8_t *dst, int size,
  111. int stride, int width, int height,
  112. int delta, const uint8_t *ctable, uint8_t *refdata)
  113. {
  114. int i, j;
  115. int code;
  116. int filled = 0;
  117. int orig_height;
  118. if(!refdata)
  119. refdata= dst;
  120. /* copy prev frame */
  121. for(i = 0; i < height; i++)
  122. memcpy(dst + (i * stride), refdata + (i * stride), width);
  123. orig_height = height;
  124. height--;
  125. dst = dst + height * stride;
  126. while((size > 0) && (height >= 0)) {
  127. code = *src++;
  128. size--;
  129. if(delta) {
  130. /* motion compensation */
  131. while(size > 0 && (code & 0xF0) == 0xF0) {
  132. if(delta == 1) {
  133. int me_idx;
  134. int me_w, me_h, me_x, me_y;
  135. uint8_t *me_plane;
  136. int corr, val;
  137. /* get block size by index */
  138. me_idx = code & 0xF;
  139. me_w = qpeg_table_w[me_idx];
  140. me_h = qpeg_table_h[me_idx];
  141. /* extract motion vector */
  142. corr = *src++;
  143. size--;
  144. val = corr >> 4;
  145. if(val > 7)
  146. val -= 16;
  147. me_x = val;
  148. val = corr & 0xF;
  149. if(val > 7)
  150. val -= 16;
  151. me_y = val;
  152. /* check motion vector */
  153. if ((me_x + filled < 0) || (me_x + me_w + filled > width) ||
  154. (height - me_y - me_h < 0) || (height - me_y > orig_height) ||
  155. (filled + me_w > width) || (height - me_h < 0))
  156. av_log(NULL, AV_LOG_ERROR, "Bogus motion vector (%i,%i), block size %ix%i at %i,%i\n",
  157. me_x, me_y, me_w, me_h, filled, height);
  158. else {
  159. /* do motion compensation */
  160. me_plane = refdata + (filled + me_x) + (height - me_y) * stride;
  161. for(j = 0; j < me_h; j++) {
  162. for(i = 0; i < me_w; i++)
  163. dst[filled + i - (j * stride)] = me_plane[i - (j * stride)];
  164. }
  165. }
  166. }
  167. code = *src++;
  168. size--;
  169. }
  170. }
  171. if(code == 0xE0) /* end-of-picture code */
  172. break;
  173. if(code > 0xE0) { /* run code: 0xE1..0xFF */
  174. int p;
  175. code &= 0x1F;
  176. p = *src++;
  177. size--;
  178. for(i = 0; i <= code; i++) {
  179. dst[filled++] = p;
  180. if(filled >= width) {
  181. filled = 0;
  182. dst -= stride;
  183. height--;
  184. if(height < 0)
  185. break;
  186. }
  187. }
  188. } else if(code >= 0xC0) { /* copy code: 0xC0..0xDF */
  189. code &= 0x1F;
  190. if(code + 1 > size)
  191. break;
  192. for(i = 0; i <= code; i++) {
  193. dst[filled++] = *src++;
  194. if(filled >= width) {
  195. filled = 0;
  196. dst -= stride;
  197. height--;
  198. if(height < 0)
  199. break;
  200. }
  201. }
  202. size -= code + 1;
  203. } else if(code >= 0x80) { /* skip code: 0x80..0xBF */
  204. int skip;
  205. code &= 0x3F;
  206. /* codes 0x80 and 0x81 are actually escape codes,
  207. skip value minus constant is in the next byte */
  208. if(!code) {
  209. skip = (*src++) + 64; size--;
  210. } else if(code == 1) {
  211. skip = (*src++) + 320; size--;
  212. } else
  213. skip = code;
  214. filled += skip;
  215. while( filled >= width) {
  216. filled -= width;
  217. dst -= stride;
  218. height--;
  219. if(height < 0)
  220. break;
  221. }
  222. } else {
  223. /* zero code treated as one-pixel skip */
  224. if(code)
  225. dst[filled++] = ctable[code & 0x7F];
  226. else
  227. filled++;
  228. if(filled >= width) {
  229. filled = 0;
  230. dst -= stride;
  231. height--;
  232. }
  233. }
  234. }
  235. }
  236. static int decode_frame(AVCodecContext *avctx,
  237. void *data, int *data_size,
  238. AVPacket *avpkt)
  239. {
  240. const uint8_t *buf = avpkt->data;
  241. int buf_size = avpkt->size;
  242. QpegContext * const a = avctx->priv_data;
  243. AVFrame * p = &a->pic;
  244. AVFrame * ref= &a->ref;
  245. uint8_t* outdata;
  246. int delta, ret = 0;
  247. const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
  248. if(ref->data[0])
  249. avctx->release_buffer(avctx, ref);
  250. FFSWAP(AVFrame, *ref, *p);
  251. p->reference= 3;
  252. if(avctx->get_buffer(avctx, p) < 0){
  253. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  254. return -1;
  255. }
  256. outdata = a->pic.data[0];
  257. if(buf[0x85] == 0x10) {
  258. ret = qpeg_decode_intra(buf+0x86, outdata, buf_size - 0x86, a->pic.linesize[0], avctx->width, avctx->height);
  259. } else {
  260. delta = buf[0x85];
  261. qpeg_decode_inter(buf+0x86, outdata, buf_size - 0x86, a->pic.linesize[0], avctx->width, avctx->height, delta, buf + 4, a->ref.data[0]);
  262. }
  263. if (ret<0)
  264. return ret;
  265. /* make the palette available on the way out */
  266. if (pal) {
  267. a->pic.palette_has_changed = 1;
  268. memcpy(a->pal, pal, AVPALETTE_SIZE);
  269. }
  270. memcpy(a->pic.data[1], a->pal, AVPALETTE_SIZE);
  271. *data_size = sizeof(AVFrame);
  272. *(AVFrame*)data = a->pic;
  273. return buf_size;
  274. }
  275. static av_cold int decode_init(AVCodecContext *avctx){
  276. QpegContext * const a = avctx->priv_data;
  277. avcodec_get_frame_defaults(&a->pic);
  278. avcodec_get_frame_defaults(&a->ref);
  279. a->avctx = avctx;
  280. avctx->pix_fmt= PIX_FMT_PAL8;
  281. return 0;
  282. }
  283. static av_cold int decode_end(AVCodecContext *avctx){
  284. QpegContext * const a = avctx->priv_data;
  285. AVFrame * const p = &a->pic;
  286. AVFrame * const ref= &a->ref;
  287. if(p->data[0])
  288. avctx->release_buffer(avctx, p);
  289. if(ref->data[0])
  290. avctx->release_buffer(avctx, ref);
  291. return 0;
  292. }
  293. AVCodec ff_qpeg_decoder = {
  294. .name = "qpeg",
  295. .type = AVMEDIA_TYPE_VIDEO,
  296. .id = CODEC_ID_QPEG,
  297. .priv_data_size = sizeof(QpegContext),
  298. .init = decode_init,
  299. .close = decode_end,
  300. .decode = decode_frame,
  301. .capabilities = CODEC_CAP_DR1,
  302. .long_name = NULL_IF_CONFIG_SMALL("Q-team QPEG"),
  303. };