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.

375 lines
12KB

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