Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Canopus HQ/HQA decoder
  3.  *
  4.  * This file is part of FFmpeg.
  5.  *
  6.  * FFmpeg is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * FFmpeg is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with FFmpeg; if not, write to the Free Software
  18.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19.  */
  20.  
  21. #include <stdint.h>
  22.  
  23. #include "libavutil/attributes.h"
  24. #include "libavutil/intreadwrite.h"
  25.  
  26. #include "avcodec.h"
  27. #include "canopus.h"
  28. #include "internal.h"
  29.  
  30. #include "hq_hqa.h"
  31. #include "hq_hqadsp.h"
  32.  
  33. /* HQ/HQA slices are a set of macroblocks belonging to a frame, and
  34.  * they usually form a pseudorandom pattern (probably because it is
  35.  * nicer to display on partial decode).
  36.  *
  37.  * For HQA it just happens that each slice is on every 8th macroblock,
  38.  * but they can be on any frame width like
  39.  *   X.......X.
  40.  *   ......X...
  41.  *   ....X.....
  42.  *   ..X.......
  43.  * etc.
  44.  *
  45.  * The original decoder has special handling for edge macroblocks,
  46.  * while lavc simply aligns coded_width and coded_height.
  47.  */
  48.  
  49. static inline void put_blocks(HQContext *c, AVFrame *pic,
  50.                               int plane, int x, int y, int ilace,
  51.                               int16_t *block0, int16_t *block1)
  52. {
  53.     uint8_t *p = pic->data[plane] + x;
  54.  
  55.     c->hqhqadsp.idct_put(p + y * pic->linesize[plane],
  56.                          pic->linesize[plane] << ilace, block0);
  57.     c->hqhqadsp.idct_put(p + (y + (ilace ? 1 : 8)) * pic->linesize[plane],
  58.                          pic->linesize[plane] << ilace, block1);
  59. }
  60.  
  61. static int hq_decode_block(HQContext *c, GetBitContext *gb, int16_t block[64],
  62.                            int qsel, int is_chroma, int is_hqa)
  63. {
  64.     const int32_t *q;
  65.     int val, pos = 1;
  66.  
  67.     memset(block, 0, 64 * sizeof(*block));
  68.  
  69.     if (!is_hqa) {
  70.         block[0] = get_sbits(gb, 9) << 6;
  71.         q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
  72.     } else {
  73.         q = ff_hq_quants[qsel][is_chroma][get_bits(gb, 2)];
  74.         block[0] = get_sbits(gb, 9) << 6;
  75.     }
  76.  
  77.     for (;;) {
  78.         val = get_vlc2(gb, c->hq_ac_vlc.table, 9, 2);
  79.         if (val < 0)
  80.             return AVERROR_INVALIDDATA;
  81.  
  82.         pos += ff_hq_ac_skips[val];
  83.         if (pos >= 64)
  84.             break;
  85.         block[ff_zigzag_direct[pos]] = (ff_hq_ac_syms[val] * q[pos]) >> 12;
  86.         pos++;
  87.     }
  88.  
  89.     return 0;
  90. }
  91.  
  92. static int hq_decode_mb(HQContext *c, AVFrame *pic,
  93.                         GetBitContext *gb, int x, int y)
  94. {
  95.     int qgroup, flag;
  96.     int i, ret;
  97.  
  98.     qgroup = get_bits(gb, 4);
  99.     flag = get_bits1(gb);
  100.  
  101.     for (i = 0; i < 8; i++) {
  102.         ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 4, 0);
  103.         if (ret < 0)
  104.             return ret;
  105.     }
  106.  
  107.     put_blocks(c, pic, 0, x,      y, flag, c->block[0], c->block[2]);
  108.     put_blocks(c, pic, 0, x + 8,  y, flag, c->block[1], c->block[3]);
  109.     put_blocks(c, pic, 2, x >> 1, y, flag, c->block[4], c->block[5]);
  110.     put_blocks(c, pic, 1, x >> 1, y, flag, c->block[6], c->block[7]);
  111.  
  112.     return 0;
  113. }
  114.  
  115. static int hq_decode_frame(HQContext *ctx, AVFrame *pic,
  116.                            int prof_num, size_t data_size)
  117. {
  118.     const HQProfile *profile;
  119.     GetBitContext gb;
  120.     const uint8_t *perm, *src = ctx->gbc.buffer;
  121.     uint32_t slice_off[21];
  122.     int slice, start_off, next_off, i, ret;
  123.  
  124.     if ((unsigned)prof_num >= NUM_HQ_PROFILES) {
  125.         profile = &ff_hq_profile[0];
  126.         avpriv_request_sample(ctx->avctx, "HQ Profile %d", prof_num);
  127.     } else {
  128.         profile = &ff_hq_profile[prof_num];
  129.         av_log(ctx->avctx, AV_LOG_VERBOSE, "HQ Profile %d\n", prof_num);
  130.     }
  131.  
  132.     ctx->avctx->coded_width         = FFALIGN(profile->width,  16);
  133.     ctx->avctx->coded_height        = FFALIGN(profile->height, 16);
  134.     ctx->avctx->width               = profile->width;
  135.     ctx->avctx->height              = profile->height;
  136.     ctx->avctx->bits_per_raw_sample = 8;
  137.     ctx->avctx->pix_fmt             = AV_PIX_FMT_YUV422P;
  138.  
  139.     ret = ff_get_buffer(ctx->avctx, pic, 0);
  140.     if (ret < 0)
  141.         return ret;
  142.  
  143.     /* Offsets are stored from CUV position, so adjust them accordingly. */
  144.     for (i = 0; i < profile->num_slices + 1; i++)
  145.         slice_off[i] = bytestream2_get_be24(&ctx->gbc) - 4;
  146.  
  147.     next_off = 0;
  148.     for (slice = 0; slice < profile->num_slices; slice++) {
  149.         start_off = next_off;
  150.         next_off  = profile->tab_h * (slice + 1) / profile->num_slices;
  151.         perm = profile->perm_tab + start_off * profile->tab_w * 2;
  152.  
  153.         if (slice_off[slice] < (profile->num_slices + 1) * 3 ||
  154.             slice_off[slice] >= slice_off[slice + 1] ||
  155.             slice_off[slice + 1] > data_size) {
  156.             av_log(ctx->avctx, AV_LOG_ERROR,
  157.                    "Invalid slice size %zu.\n", data_size);
  158.             break;
  159.         }
  160.         init_get_bits(&gb, src + slice_off[slice],
  161.                       (slice_off[slice + 1] - slice_off[slice]) * 8);
  162.  
  163.         for (i = 0; i < (next_off - start_off) * profile->tab_w; i++) {
  164.             ret = hq_decode_mb(ctx, pic, &gb, perm[0] * 16, perm[1] * 16);
  165.             if (ret < 0) {
  166.                 av_log(ctx->avctx, AV_LOG_ERROR,
  167.                        "Error decoding macroblock %d at slice %d.\n", i, slice);
  168.                 return ret;
  169.             }
  170.             perm += 2;
  171.         }
  172.     }
  173.  
  174.     return 0;
  175. }
  176.  
  177. static int hqa_decode_mb(HQContext *c, AVFrame *pic, int qgroup,
  178.                          GetBitContext *gb, int x, int y)
  179. {
  180.     int flag = 0;
  181.     int i, ret, cbp;
  182.  
  183.     cbp = get_vlc2(gb, c->hqa_cbp_vlc.table, 5, 1);
  184.  
  185.     for (i = 0; i < 12; i++)
  186.         memset(c->block[i], 0, sizeof(*c->block));
  187.     for (i = 0; i < 12; i++)
  188.         c->block[i][0] = -128 * (1 << 6);
  189.  
  190.     if (cbp) {
  191.         flag = get_bits1(gb);
  192.  
  193.         cbp |= cbp << 4;
  194.         if (cbp & 0x3)
  195.             cbp |= 0x500;
  196.         if (cbp & 0xC)
  197.             cbp |= 0xA00;
  198.         for (i = 0; i < 12; i++) {
  199.             if (!(cbp & (1 << i)))
  200.                 continue;
  201.             ret = hq_decode_block(c, gb, c->block[i], qgroup, i >= 8, 1);
  202.             if (ret < 0)
  203.                 return ret;
  204.         }
  205.     }
  206.  
  207.     put_blocks(c, pic, 3, x,      y, flag, c->block[ 0], c->block[ 2]);
  208.     put_blocks(c, pic, 3, x + 8,  y, flag, c->block[ 1], c->block[ 3]);
  209.     put_blocks(c, pic, 0, x,      y, flag, c->block[ 4], c->block[ 6]);
  210.     put_blocks(c, pic, 0, x + 8,  y, flag, c->block[ 5], c->block[ 7]);
  211.     put_blocks(c, pic, 2, x >> 1, y, flag, c->block[ 8], c->block[ 9]);
  212.     put_blocks(c, pic, 1, x >> 1, y, flag, c->block[10], c->block[11]);
  213.  
  214.     return 0;
  215. }
  216.  
  217. static int hqa_decode_slice(HQContext *ctx, AVFrame *pic, GetBitContext *gb,
  218.                             int quant, int slice_no, int w, int h)
  219. {
  220.     int i, j, off;
  221.     int ret;
  222.  
  223.     for (i = 0; i < h; i += 16) {
  224.         off = (slice_no * 16 + i * 3) & 0x70;
  225.         for (j = off; j < w; j += 128) {
  226.             ret = hqa_decode_mb(ctx, pic, quant, gb, j, i);
  227.             if (ret < 0) {
  228.                 av_log(ctx->avctx, AV_LOG_ERROR,
  229.                        "Error decoding macroblock at %dx%d.\n", i, j);
  230.                 return ret;
  231.             }
  232.         }
  233.     }
  234.  
  235.     return 0;
  236. }
  237.  
  238. static int hqa_decode_frame(HQContext *ctx, AVFrame *pic, size_t data_size)
  239. {
  240.     GetBitContext gb;
  241.     const int num_slices = 8;
  242.     uint32_t slice_off[9];
  243.     int i, slice, ret;
  244.     int width, height, quant;
  245.     const uint8_t *src = ctx->gbc.buffer;
  246.  
  247.     width  = bytestream2_get_be16(&ctx->gbc);
  248.     height = bytestream2_get_be16(&ctx->gbc);
  249.  
  250.     ctx->avctx->coded_width         = FFALIGN(width,  16);
  251.     ctx->avctx->coded_height        = FFALIGN(height, 16);
  252.     ctx->avctx->width               = width;
  253.     ctx->avctx->height              = height;
  254.     ctx->avctx->bits_per_raw_sample = 8;
  255.     ctx->avctx->pix_fmt             = AV_PIX_FMT_YUVA422P;
  256.  
  257.     av_log(ctx->avctx, AV_LOG_VERBOSE, "HQA Profile\n");
  258.  
  259.     quant = bytestream2_get_byte(&ctx->gbc);
  260.     bytestream2_skip(&ctx->gbc, 3);
  261.     if (quant >= NUM_HQ_QUANTS) {
  262.         av_log(ctx->avctx, AV_LOG_ERROR,
  263.                "Invalid quantization matrix %d.\n", quant);
  264.         return AVERROR_INVALIDDATA;
  265.     }
  266.  
  267.     ret = ff_get_buffer(ctx->avctx, pic, 0);
  268.     if (ret < 0)
  269.         return ret;
  270.  
  271.     /* Offsets are stored from HQA1 position, so adjust them accordingly. */
  272.     for (i = 0; i < num_slices + 1; i++)
  273.         slice_off[i] = bytestream2_get_be32(&ctx->gbc) - 4;
  274.  
  275.     for (slice = 0; slice < num_slices; slice++) {
  276.         if (slice_off[slice] < (num_slices + 1) * 3 ||
  277.             slice_off[slice] >= slice_off[slice + 1] ||
  278.             slice_off[slice + 1] > data_size) {
  279.             av_log(ctx->avctx, AV_LOG_ERROR,
  280.                    "Invalid slice size %zu.\n", data_size);
  281.             break;
  282.         }
  283.         init_get_bits(&gb, src + slice_off[slice],
  284.                       (slice_off[slice + 1] - slice_off[slice]) * 8);
  285.  
  286.         ret = hqa_decode_slice(ctx, pic, &gb, quant, slice, width, height);
  287.         if (ret < 0)
  288.             return ret;
  289.     }
  290.  
  291.     return 0;
  292. }
  293.  
  294. static int hq_hqa_decode_frame(AVCodecContext *avctx, void *data,
  295.                                int *got_frame, AVPacket *avpkt)
  296. {
  297.     HQContext *ctx = avctx->priv_data;
  298.     AVFrame *pic = data;
  299.     uint32_t info_tag;
  300.     unsigned int data_size;
  301.     int ret;
  302.     unsigned tag;
  303.  
  304.     bytestream2_init(&ctx->gbc, avpkt->data, avpkt->size);
  305.     if (bytestream2_get_bytes_left(&ctx->gbc) < 4 + 4) {
  306.         av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", avpkt->size);
  307.         return AVERROR_INVALIDDATA;
  308.     }
  309.  
  310.     info_tag = bytestream2_peek_le32(&ctx->gbc);
  311.     if (info_tag == MKTAG('I', 'N', 'F', 'O')) {
  312.         int info_size;
  313.         bytestream2_skip(&ctx->gbc, 4);
  314.         info_size = bytestream2_get_le32(&ctx->gbc);
  315.         if (bytestream2_get_bytes_left(&ctx->gbc) < info_size) {
  316.             av_log(avctx, AV_LOG_ERROR, "Invalid INFO size (%d).\n", info_size);
  317.             return AVERROR_INVALIDDATA;
  318.         }
  319.         ff_canopus_parse_info_tag(avctx, ctx->gbc.buffer, info_size);
  320.  
  321.         bytestream2_skip(&ctx->gbc, info_size);
  322.     }
  323.  
  324.     data_size = bytestream2_get_bytes_left(&ctx->gbc);
  325.     if (data_size < 4) {
  326.         av_log(avctx, AV_LOG_ERROR, "Frame is too small (%d).\n", data_size);
  327.         return AVERROR_INVALIDDATA;
  328.     }
  329.  
  330.     /* HQ defines dimensions and number of slices, and thus slice traversal
  331.      * order. HQA has no size constraint and a fixed number of slices, so it
  332.      * needs a separate scheme for it. */
  333.     tag = bytestream2_get_le32(&ctx->gbc);
  334.     if ((tag & 0x00FFFFFF) == (MKTAG('U', 'V', 'C', ' ') & 0x00FFFFFF)) {
  335.         ret = hq_decode_frame(ctx, pic, tag >> 24, data_size);
  336.     } else if (tag == MKTAG('H', 'Q', 'A', '1')) {
  337.         ret = hqa_decode_frame(ctx, pic, data_size);
  338.     } else {
  339.         av_log(avctx, AV_LOG_ERROR, "Not a HQ/HQA frame.\n");
  340.         return AVERROR_INVALIDDATA;
  341.     }
  342.     if (ret < 0) {
  343.         av_log(avctx, AV_LOG_ERROR, "Error decoding frame.\n");
  344.         return ret;
  345.     }
  346.  
  347.     pic->key_frame = 1;
  348.     pic->pict_type = AV_PICTURE_TYPE_I;
  349.  
  350.     *got_frame = 1;
  351.  
  352.     return avpkt->size;
  353. }
  354.  
  355. static av_cold int hq_hqa_decode_init(AVCodecContext *avctx)
  356. {
  357.     HQContext *ctx = avctx->priv_data;
  358.     ctx->avctx = avctx;
  359.  
  360.     ff_hqdsp_init(&ctx->hqhqadsp);
  361.  
  362.     return ff_hq_init_vlcs(ctx);
  363. }
  364.  
  365. static av_cold int hq_hqa_decode_close(AVCodecContext *avctx)
  366. {
  367.     HQContext *ctx = avctx->priv_data;
  368.  
  369.     ff_free_vlc(&ctx->hq_ac_vlc);
  370.     ff_free_vlc(&ctx->hqa_cbp_vlc);
  371.  
  372.     return 0;
  373. }
  374.  
  375. AVCodec ff_hq_hqa_decoder = {
  376.     .name           = "hq_hqa",
  377.     .long_name      = NULL_IF_CONFIG_SMALL("Canopus HQ/HQA"),
  378.     .type           = AVMEDIA_TYPE_VIDEO,
  379.     .id             = AV_CODEC_ID_HQ_HQA,
  380.     .priv_data_size = sizeof(HQContext),
  381.     .init           = hq_hqa_decode_init,
  382.     .decode         = hq_hqa_decode_frame,
  383.     .close          = hq_hqa_decode_close,
  384.     .capabilities   = AV_CODEC_CAP_DR1,
  385.     .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE |
  386.                       FF_CODEC_CAP_INIT_CLEANUP,
  387. };
  388.