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.

356 lines
11KB

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