1/*
2 * H.263i 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 "codec_internal.h"
22#include "mpegutils.h"
23#include "mpegvideo.h"
24#include "mpegvideodec.h"
25#include "h263data.h"
26#include "h263dec.h"
27#include "mpegvideodata.h"
28
29/* don't understand why they choose a different header ! */
30int ff_intel_h263_decode_picture_header(MpegEncContext *s)
31{
32    int format;
33
34    if (get_bits_left(&s->gb) == 64) { /* special dummy frames */
35        return FRAME_SKIPPED;
36    }
37
38    /* picture header */
39    if (get_bits(&s->gb, 22) != 0x20) {
40        av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
41        return -1;
42    }
43    s->picture_number = get_bits(&s->gb, 8); /* picture timestamp */
44
45    if (check_marker(s->avctx, &s->gb, "after picture_number") != 1) {
46        return -1;      /* marker */
47    }
48    if (get_bits1(&s->gb) != 0) {
49        av_log(s->avctx, AV_LOG_ERROR, "Bad H.263 id\n");
50        return -1;      /* H.263 id */
51    }
52    skip_bits1(&s->gb);         /* split screen off */
53    skip_bits1(&s->gb);         /* camera  off */
54    skip_bits1(&s->gb);         /* freeze picture release off */
55
56    format = get_bits(&s->gb, 3);
57    if (format == 0 || format == 6) {
58        av_log(s->avctx, AV_LOG_ERROR, "Intel H.263 free format not supported\n");
59        return -1;
60    }
61    s->h263_plus = 0;
62
63    s->pict_type = AV_PICTURE_TYPE_I + get_bits1(&s->gb);
64
65    s->h263_long_vectors = get_bits1(&s->gb);
66
67    if (get_bits1(&s->gb) != 0) {
68        av_log(s->avctx, AV_LOG_ERROR, "SAC not supported\n");
69        return -1;      /* SAC: off */
70    }
71    s->obmc= get_bits1(&s->gb);
72    s->pb_frame = get_bits1(&s->gb);
73
74    if (format < 6) {
75        s->width = ff_h263_format[format][0];
76        s->height = ff_h263_format[format][1];
77        s->avctx->sample_aspect_ratio.num = 12;
78        s->avctx->sample_aspect_ratio.den = 11;
79    } else {
80        format = get_bits(&s->gb, 3);
81        if(format == 0 || format == 7){
82            av_log(s->avctx, AV_LOG_ERROR, "Wrong Intel H.263 format\n");
83            return -1;
84        }
85        if(get_bits(&s->gb, 2))
86            av_log(s->avctx, AV_LOG_ERROR, "Bad value for reserved field\n");
87        s->loop_filter = get_bits1(&s->gb) * !s->avctx->lowres;
88        if(get_bits1(&s->gb))
89            av_log(s->avctx, AV_LOG_ERROR, "Bad value for reserved field\n");
90        if(get_bits1(&s->gb))
91            s->pb_frame = 2;
92        if(get_bits(&s->gb, 5))
93            av_log(s->avctx, AV_LOG_ERROR, "Bad value for reserved field\n");
94        if(get_bits(&s->gb, 5) != 1)
95            av_log(s->avctx, AV_LOG_ERROR, "Invalid marker\n");
96    }
97    if(format == 6){
98        int ar = get_bits(&s->gb, 4);
99        skip_bits(&s->gb, 9); // display width
100        check_marker(s->avctx, &s->gb, "in dimensions");
101        skip_bits(&s->gb, 9); // display height
102        if(ar == 15){
103            s->avctx->sample_aspect_ratio.num = get_bits(&s->gb, 8); // aspect ratio - width
104            s->avctx->sample_aspect_ratio.den = get_bits(&s->gb, 8); // aspect ratio - height
105        } else {
106            s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[ar];
107        }
108        if (s->avctx->sample_aspect_ratio.num == 0)
109            av_log(s->avctx, AV_LOG_ERROR, "Invalid aspect ratio.\n");
110    }
111
112    s->chroma_qscale= s->qscale = get_bits(&s->gb, 5);
113    skip_bits1(&s->gb); /* Continuous Presence Multipoint mode: off */
114
115    if(s->pb_frame){
116        skip_bits(&s->gb, 3); //temporal reference for B-frame
117        skip_bits(&s->gb, 2); //dbquant
118    }
119
120    /* PEI */
121    if (skip_1stop_8data_bits(&s->gb) < 0)
122        return AVERROR_INVALIDDATA;
123    s->f_code = 1;
124
125    s->y_dc_scale_table=
126    s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
127
128    ff_h263_show_pict_info(s);
129
130    return 0;
131}
132
133const FFCodec ff_h263i_decoder = {
134    .p.name         = "h263i",
135    .p.long_name    = NULL_IF_CONFIG_SMALL("Intel H.263"),
136    .p.type         = AVMEDIA_TYPE_VIDEO,
137    .p.id           = AV_CODEC_ID_H263I,
138    .priv_data_size = sizeof(MpegEncContext),
139    .init           = ff_h263_decode_init,
140    .close          = ff_h263_decode_end,
141    FF_CODEC_DECODE_CB(ff_h263_decode_frame),
142    .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1,
143    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE |
144                      FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
145    .p.pix_fmts     = (const enum AVPixelFormat[]) {
146        AV_PIX_FMT_YUV420P,
147        AV_PIX_FMT_NONE
148    },
149};
150