1/*
2 * FLV decoding.
3 * This file is part of FFmpeg.
4 *
5 * FFmpeg 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.1 of the License, or (at your option) any later version.
9 *
10 * FFmpeg 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 FFmpeg; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include "mpegvideo.h"
21#include "h263.h"
22#include "flv.h"
23#include "libavutil/imgutils.h"
24
25void ff_flv2_decode_ac_esc(GetBitContext *gb, int *level, int *run, int *last)
26{
27    int is11 = get_bits1(gb);
28    *last = get_bits1(gb);
29    *run  = get_bits(gb, 6);
30    if (is11) {
31        *level = get_sbits(gb, 11);
32    } else {
33        *level = get_sbits(gb, 7);
34    }
35}
36
37int ff_flv_decode_picture_header(MpegEncContext *s)
38{
39    int format, width, height;
40
41    /* picture header */
42    if (get_bits_long(&s->gb, 17) != 1) {
43        av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
44        return AVERROR_INVALIDDATA;
45    }
46    format = get_bits(&s->gb, 5);
47    if (format != 0 && format != 1) {
48        av_log(s->avctx, AV_LOG_ERROR, "Bad picture format\n");
49        return AVERROR_INVALIDDATA;
50    }
51    s->h263_flv       = format + 1;
52    s->picture_number = get_bits(&s->gb, 8); /* picture timestamp */
53    format            = get_bits(&s->gb, 3);
54    switch (format) {
55    case 0:
56        width  = get_bits(&s->gb, 8);
57        height = get_bits(&s->gb, 8);
58        break;
59    case 1:
60        width  = get_bits(&s->gb, 16);
61        height = get_bits(&s->gb, 16);
62        break;
63    case 2:
64        width  = 352;
65        height = 288;
66        break;
67    case 3:
68        width  = 176;
69        height = 144;
70        break;
71    case 4:
72        width  = 128;
73        height = 96;
74        break;
75    case 5:
76        width  = 320;
77        height = 240;
78        break;
79    case 6:
80        width  = 160;
81        height = 120;
82        break;
83    default:
84        width = height = 0;
85        break;
86    }
87    if (av_image_check_size(width, height, 0, s->avctx))
88        return AVERROR(EINVAL);
89    s->width  = width;
90    s->height = height;
91
92    s->pict_type = AV_PICTURE_TYPE_I + get_bits(&s->gb, 2);
93    s->droppable = s->pict_type > AV_PICTURE_TYPE_P;
94    if (s->droppable)
95        s->pict_type = AV_PICTURE_TYPE_P;
96
97    skip_bits1(&s->gb); /* deblocking flag */
98    s->chroma_qscale = s->qscale = get_bits(&s->gb, 5);
99
100    s->h263_plus = 0;
101
102    s->unrestricted_mv   = 1;
103    s->h263_long_vectors = 0;
104
105    /* PEI */
106    if (skip_1stop_8data_bits(&s->gb) < 0)
107        return AVERROR_INVALIDDATA;
108
109    s->f_code = 1;
110
111    if (s->ehc_mode)
112        s->avctx->sample_aspect_ratio= (AVRational){1,2};
113
114    if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
115        av_log(s->avctx, AV_LOG_DEBUG, "%c esc_type:%d, qp:%d num:%d\n",
116               s->droppable ? 'D' : av_get_picture_type_char(s->pict_type),
117               s->h263_flv - 1, s->qscale, s->picture_number);
118    }
119
120    s->y_dc_scale_table = s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
121
122    return 0;
123}
124
125AVCodec ff_flv_decoder = {
126    .name           = "flv",
127    .long_name      = NULL_IF_CONFIG_SMALL("FLV / Sorenson Spark / Sorenson H.263 (Flash Video)"),
128    .type           = AVMEDIA_TYPE_VIDEO,
129    .id             = AV_CODEC_ID_FLV1,
130    .priv_data_size = sizeof(MpegEncContext),
131    .init           = ff_h263_decode_init,
132    .close          = ff_h263_decode_end,
133    .decode         = ff_h263_decode_frame,
134    .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
135    .max_lowres     = 3,
136    .pix_fmts       = (const enum AVPixelFormat[]) {
137        AV_PIX_FMT_YUV420P,
138        AV_PIX_FMT_NONE
139    },
140};
141