annotate h261.c @ 2059:ad972ab280bc libavcodec

sample_rate and channels in aac audio patch by (Luca Abeni <lucabe72 at email dot it>)
author michael
date Sat, 05 Jun 2004 22:36:43 +0000
parents c89a3019e088
children 8afd396fa198
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2044
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
1 /*
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
2 * H261 decoder
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
3 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
4 * Copyright (c) 2004 Maarten Daniels
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
5 *
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
6 * This library is free software; you can redistribute it and/or
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
7 * modify it under the terms of the GNU Lesser General Public
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
8 * License as published by the Free Software Foundation; either
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
9 * version 2 of the License, or (at your option) any later version.
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
10 *
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
11 * This library is distributed in the hope that it will be useful,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
14 * Lesser General Public License for more details.
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
15 *
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
16 * You should have received a copy of the GNU Lesser General Public
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
17 * License along with this library; if not, write to the Free Software
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
19 */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
20
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
21 /**
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
22 * @file h261.c
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
23 * h261codec.
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
24 */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
25
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
26 #include "common.h"
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
27 #include "dsputil.h"
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
28 #include "avcodec.h"
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
29 #include "mpegvideo.h"
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
30 #include "h261data.h"
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
31
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
32
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
33 #define H261_MBA_VLC_BITS 9
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
34 #define H261_MTYPE_VLC_BITS 6
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
35 #define H261_MV_VLC_BITS 7
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
36 #define H261_CBP_VLC_BITS 9
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
37 #define TCOEFF_VLC_BITS 9
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
38
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
39 #define MAX_MBA 33
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
40 #define IS_FIL(a) ((a)&MB_TYPE_H261_FIL)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
41
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
42 /**
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
43 * H261Context
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
44 */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
45 typedef struct H261Context{
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
46 MpegEncContext s;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
47
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
48 int current_mba;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
49 int mba_diff;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
50 int mtype;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
51 int current_mv_x;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
52 int current_mv_y;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
53 int gob_number;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
54 int loop_filter;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
55 int bits_left; //8 - nr of bits left of the following frame in the last byte in this frame
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
56 int last_bits; //bits left of the following frame in the last byte in this frame
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
57 }H261Context;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
58
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
59 void ff_h261_loop_filter(H261Context * h){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
60 MpegEncContext * const s = &h->s;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
61 const int linesize = s->linesize;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
62 const int uvlinesize= s->uvlinesize;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
63 uint8_t *dest_y = s->dest[0];
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
64 uint8_t *dest_cb= s->dest[1];
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
65 uint8_t *dest_cr= s->dest[2];
2045
9447bbd8a7e9 rewrite h261 loop filter
michael
parents: 2044
diff changeset
66
9447bbd8a7e9 rewrite h261 loop filter
michael
parents: 2044
diff changeset
67 s->dsp.h261_loop_filter(dest_y , linesize);
9447bbd8a7e9 rewrite h261 loop filter
michael
parents: 2044
diff changeset
68 s->dsp.h261_loop_filter(dest_y + 8, linesize);
9447bbd8a7e9 rewrite h261 loop filter
michael
parents: 2044
diff changeset
69 s->dsp.h261_loop_filter(dest_y + 8 * linesize , linesize);
9447bbd8a7e9 rewrite h261 loop filter
michael
parents: 2044
diff changeset
70 s->dsp.h261_loop_filter(dest_y + 8 * linesize + 8, linesize);
9447bbd8a7e9 rewrite h261 loop filter
michael
parents: 2044
diff changeset
71 s->dsp.h261_loop_filter(dest_cb, uvlinesize);
9447bbd8a7e9 rewrite h261 loop filter
michael
parents: 2044
diff changeset
72 s->dsp.h261_loop_filter(dest_cr, uvlinesize);
2044
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
73 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
74
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
75 static int h261_decode_block(H261Context *h, DCTELEM *block,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
76 int n, int coded);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
77 static int h261_decode_mb(H261Context *h,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
78 DCTELEM block[6][64]);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
79 void ff_set_qscale(MpegEncContext * s, int qscale);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
80
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
81 /***********************************************/
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
82 /* decoding */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
83
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
84 static VLC h261_mba_vlc;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
85 static VLC h261_mtype_vlc;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
86 static VLC h261_mv_vlc;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
87 static VLC h261_cbp_vlc;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
88
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
89 void init_vlc_rl(RLTable *rl);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
90
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
91 static void h261_decode_init_vlc(H261Context *h){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
92 static int done = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
93
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
94 if(!done){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
95 done = 1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
96 init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 34,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
97 h261_mba_bits, 1, 1,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
98 h261_mba_code, 1, 1);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
99 init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
100 h261_mtype_bits, 1, 1,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
101 h261_mtype_code, 1, 1);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
102 init_vlc(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
103 &h261_mv_tab[0][1], 2, 1,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
104 &h261_mv_tab[0][0], 2, 1);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
105 init_vlc(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
106 &h261_cbp_tab[0][1], 2, 1,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
107 &h261_cbp_tab[0][0], 2, 1);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
108 init_rl(&h261_rl_tcoeff);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
109 init_vlc_rl(&h261_rl_tcoeff);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
110 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
111 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
112
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
113 static int h261_decode_init(AVCodecContext *avctx){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
114 H261Context *h= avctx->priv_data;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
115 MpegEncContext * const s = &h->s;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
116
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
117 // set defaults
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
118 MPV_decode_defaults(s);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
119 s->avctx = avctx;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
120
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
121 s->width = s->avctx->width;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
122 s->height = s->avctx->height;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
123 s->codec_id = s->avctx->codec->id;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
124
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
125 s->out_format = FMT_H261;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
126 s->low_delay= 1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
127 avctx->pix_fmt= PIX_FMT_YUV420P;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
128
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
129 s->codec_id= avctx->codec->id;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
130
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
131 h261_decode_init_vlc(h);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
132
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
133 h->bits_left = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
134 h->last_bits = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
135
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
136 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
137 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
138
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
139 /**
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
140 * decodes the group of blocks header or slice header.
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
141 * @return <0 if an error occured
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
142 */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
143 static int h261_decode_gob_header(H261Context *h){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
144 unsigned int val;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
145 MpegEncContext * const s = &h->s;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
146
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
147 /* Check for GOB Start Code */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
148 val = show_bits(&s->gb, 15);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
149 if(val)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
150 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
151
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
152 /* We have a GBSC */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
153 skip_bits(&s->gb, 16);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
154
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
155 h->gob_number = get_bits(&s->gb, 4); /* GN */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
156 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
157
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
158 /* GEI */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
159 while (get_bits1(&s->gb) != 0) {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
160 skip_bits(&s->gb, 8);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
161 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
162
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
163 if(s->qscale==0)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
164 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
165
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
166 // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
167 // subsequent macroblocks, MBA is the difference between the absolute addresses of
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
168 // the macroblock and the last transmitted macroblock.
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
169 h->current_mba = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
170 h->mba_diff = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
171
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
172 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
173 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
174
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
175 /**
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
176 * decodes the group of blocks / video packet header.
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
177 * @return <0 if no resync found
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
178 */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
179 static int ff_h261_resync(H261Context *h){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
180 MpegEncContext * const s = &h->s;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
181 int left, ret;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
182
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
183 if(show_bits(&s->gb, 15)==0){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
184 ret= h261_decode_gob_header(h);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
185 if(ret>=0)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
186 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
187 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
188 //ok, its not where its supposed to be ...
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
189 s->gb= s->last_resync_gb;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
190 align_get_bits(&s->gb);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
191 left= s->gb.size_in_bits - get_bits_count(&s->gb);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
192
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
193 for(;left>15+1+4+5; left-=8){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
194 if(show_bits(&s->gb, 15)==0){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
195 GetBitContext bak= s->gb;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
196
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
197 ret= h261_decode_gob_header(h);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
198 if(ret>=0)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
199 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
200
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
201 s->gb= bak;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
202 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
203 skip_bits(&s->gb, 8);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
204 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
205
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
206 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
207 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
208
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
209 /**
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
210 * decodes a skipped macroblock, called when when mba_diff > 1.
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
211 * @return 0
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
212 */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
213 static int h261_decode_mb_skipped(H261Context *h,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
214 DCTELEM block[6][64])
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
215 {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
216 MpegEncContext * const s = &h->s;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
217 int i;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
218 const int xy = s->mb_x + s->mb_y * s->mb_stride;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
219 s->mb_intra = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
220 for(i=0;i<6;i++)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
221 s->block_last_index[i] = -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
222 s->mv_dir = MV_DIR_FORWARD;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
223 s->mv_type = MV_TYPE_16X16;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
224 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
225 s->mv[0][0][0] = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
226 s->mv[0][0][1] = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
227 s->mb_skiped = 1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
228 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
229 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
230
2046
863ce5459aec simplify
michael
parents: 2045
diff changeset
231 static int decode_mv_component(GetBitContext *gb, int v){
863ce5459aec simplify
michael
parents: 2045
diff changeset
232 int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
863ce5459aec simplify
michael
parents: 2045
diff changeset
233 mv_diff = mvmap[mv_diff];
863ce5459aec simplify
michael
parents: 2045
diff changeset
234
863ce5459aec simplify
michael
parents: 2045
diff changeset
235 if(mv_diff && !get_bits1(gb))
863ce5459aec simplify
michael
parents: 2045
diff changeset
236 mv_diff= -mv_diff;
863ce5459aec simplify
michael
parents: 2045
diff changeset
237
863ce5459aec simplify
michael
parents: 2045
diff changeset
238 v += mv_diff;
863ce5459aec simplify
michael
parents: 2045
diff changeset
239 if (v <=-16) v+= 32;
863ce5459aec simplify
michael
parents: 2045
diff changeset
240 else if(v >= 16) v-= 32;
863ce5459aec simplify
michael
parents: 2045
diff changeset
241
863ce5459aec simplify
michael
parents: 2045
diff changeset
242 return v;
863ce5459aec simplify
michael
parents: 2045
diff changeset
243 }
863ce5459aec simplify
michael
parents: 2045
diff changeset
244
2044
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
245 static int h261_decode_mb(H261Context *h,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
246 DCTELEM block[6][64])
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
247 {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
248 MpegEncContext * const s = &h->s;
2050
c89a3019e088 10l fix by (Wolfram Gloger <wmglo at dent dot med dot uni-muenchen dot de>)
michael
parents: 2047
diff changeset
249 int i, cbp, xy, old_mtype;
2044
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
250
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
251 cbp = 63;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
252 // Read mba
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
253 do{
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
254 h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2)+1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
255 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
256 while( h->mba_diff == MAX_MBA + 1 ); // stuffing
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
257
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
258 if ( h->mba_diff < 0 )
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
259 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
260
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
261 h->current_mba += h->mba_diff;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
262
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
263 if ( h->current_mba > MAX_MBA )
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
264 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
265
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
266 s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
267 s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
268
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
269 xy = s->mb_x + s->mb_y * s->mb_stride;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
270
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
271 ff_init_block_index(s);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
272 ff_update_block_index(s);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
273
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
274 // Read mtype
2050
c89a3019e088 10l fix by (Wolfram Gloger <wmglo at dent dot med dot uni-muenchen dot de>)
michael
parents: 2047
diff changeset
275 old_mtype = h->mtype;
2044
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
276 h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
277 h->mtype = h261_mtype_map[h->mtype];
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
278
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
279 if (IS_FIL (h->mtype))
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
280 h->loop_filter = 1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
281
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
282 // Read mquant
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
283 if ( IS_QUANT ( h->mtype ) ){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
284 s->qscale = get_bits(&s->gb, 5);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
285 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
286
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
287 s->mb_intra = IS_INTRA4x4(h->mtype);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
288
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
289 // Read mv
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
290 if ( IS_16X16 ( h->mtype ) ){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
291 // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
292 // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
293 // following three situations:
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
294 // 1) evaluating MVD for macroblocks 1, 12 and 23;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
295 // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
296 // 3) MTYPE of the previous macroblock was not MC.
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
297 if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
298 ( h->mba_diff != 1) || ( !IS_16X16 ( old_mtype ) ))
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
299 {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
300 h->current_mv_x = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
301 h->current_mv_y = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
302 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
303
2046
863ce5459aec simplify
michael
parents: 2045
diff changeset
304 h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
863ce5459aec simplify
michael
parents: 2045
diff changeset
305 h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
2044
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
306 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
307
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
308 // Read cbp
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
309 if ( HAS_CBP( h->mtype ) ){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
310 cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
311 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
312
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
313 if(s->mb_intra){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
314 s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
315 goto intra;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
316 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
317
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
318 //set motion vectors
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
319 s->mv_dir = MV_DIR_FORWARD;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
320 s->mv_type = MV_TYPE_16X16;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
321 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
322 if(IS_16X16 ( h->mtype )){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
323 s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
324 s->mv[0][0][1] = h->current_mv_y * 2;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
325 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
326 else{
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
327 h->current_mv_x = s->mv[0][0][0] = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
328 h->current_mv_x = s->mv[0][0][1] = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
329 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
330
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
331 intra:
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
332 /* decode each block */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
333 if(s->mb_intra || HAS_CBP(h->mtype)){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
334 for (i = 0; i < 6; i++) {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
335 if (h261_decode_block(h, block[i], i, cbp&32) < 0){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
336 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
337 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
338 cbp+=cbp;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
339 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
340 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
341
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
342 /* per-MB end of slice check */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
343 {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
344 int v= show_bits(&s->gb, 15);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
345
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
346 if(get_bits_count(&s->gb) + 15 > s->gb.size_in_bits){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
347 v>>= get_bits_count(&s->gb) + 15 - s->gb.size_in_bits;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
348 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
349
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
350 if(v==0){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
351 return SLICE_END;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
352 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
353 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
354 return SLICE_OK;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
355 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
356
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
357 /**
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
358 * decodes a macroblock
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
359 * @return <0 if an error occured
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
360 */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
361 static int h261_decode_block(H261Context * h, DCTELEM * block,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
362 int n, int coded)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
363 {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
364 MpegEncContext * const s = &h->s;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
365 int code, level, i, j, run;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
366 RLTable *rl = &h261_rl_tcoeff;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
367 const uint8_t *scan_table;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
368
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
369 // For the variable length encoding there are two code tables, one being used for
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
370 // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
371 // for all other LEVELs except the first one in INTRA blocks which is fixed length
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
372 // coded with 8 bits.
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
373 // NOTE: the two code tables only differ in one VLC so we handle that manually.
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
374 scan_table = s->intra_scantable.permutated;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
375 if (s->mb_intra){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
376 /* DC coef */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
377 level = get_bits(&s->gb, 8);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
378 // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
379 if((level&0x7F) == 0){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
380 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
381 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
382 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
383 // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
384 if (level == 255)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
385 level = 128;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
386 block[0] = level;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
387 i = 1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
388 }else if(coded){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
389 // Run Level Code
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
390 // EOB Not possible for first level when cbp is available (that's why the table is different)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
391 // 0 1 1s
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
392 // * * 0*
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
393 int check = show_bits(&s->gb, 2);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
394 i = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
395 if ( check & 0x2 ){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
396 skip_bits(&s->gb, 2);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
397 block[0] = ( check & 0x1 ) ? -1 : 1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
398 i = 1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
399 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
400 }else{
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
401 i = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
402 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
403 if(!coded){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
404 s->block_last_index[n] = i - 1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
405 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
406 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
407 for(;;){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
408 code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
409 if (code < 0){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
410 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
411 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
412 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
413 if (code == rl->n) {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
414 /* escape */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
415 // The remaining combinations of (run, level) are encoded with a 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits level.
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
416 run = get_bits(&s->gb, 6);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
417 level = (int8_t)get_bits(&s->gb, 8);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
418 }else if(code == 0){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
419 break;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
420 }else{
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
421 run = rl->table_run[code];
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
422 level = rl->table_level[code];
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
423 if (get_bits1(&s->gb))
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
424 level = -level;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
425 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
426 i += run;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
427 if (i >= 64){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
428 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
429 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
430 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
431 j = scan_table[i];
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
432 block[j] = level;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
433 i++;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
434 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
435 s->block_last_index[n] = i;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
436 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
437 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
438
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
439 /**
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
440 * decodes the H261 picture header.
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
441 * @return <0 if no startcode found
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
442 */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
443 int h261_decode_picture_header(H261Context *h){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
444 MpegEncContext * const s = &h->s;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
445 int format, i;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
446 static int h261_framecounter = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
447 uint32_t startcode;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
448 align_get_bits(&s->gb);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
449
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
450 startcode = (h->last_bits << (12 - (8-h->bits_left))) | get_bits(&s->gb, 20-8 - (8- h->bits_left));
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
451
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
452 for(i= s->gb.size_in_bits - get_bits_count(&s->gb); i>24; i-=1){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
453 startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
454
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
455 if(startcode == 0x10)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
456 break;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
457 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
458
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
459 if (startcode != 0x10){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
460 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
461 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
462 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
463
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
464 /* temporal reference */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
465 s->picture_number = get_bits(&s->gb, 5); /* picture timestamp */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
466
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
467 /* PTYPE starts here */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
468 skip_bits1(&s->gb); /* split screen off */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
469 skip_bits1(&s->gb); /* camera off */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
470 skip_bits1(&s->gb); /* freeze picture release off */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
471
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
472 format = get_bits1(&s->gb);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
473
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
474 //only 2 formats possible
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
475 if (format == 0){//QCIF
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
476 s->width = 176;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
477 s->height = 144;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
478 s->mb_width = 11;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
479 s->mb_height = 9;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
480 }else{//CIF
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
481 s->width = 352;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
482 s->height = 288;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
483 s->mb_width = 22;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
484 s->mb_height = 18;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
485 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
486
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
487 s->mb_num = s->mb_width * s->mb_height;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
488
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
489 skip_bits1(&s->gb); /* still image mode off */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
490 skip_bits1(&s->gb); /* Reserved */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
491
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
492 /* PEI */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
493 while (get_bits1(&s->gb) != 0){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
494 skip_bits(&s->gb, 8);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
495 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
496
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
497 //h261 has no I-FRAMES, pass the test in MPV_frame_start in mpegvideo.c
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
498 if(h261_framecounter > 1)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
499 s->pict_type = P_TYPE;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
500 else
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
501 s->pict_type = I_TYPE;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
502
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
503 h261_framecounter++;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
504
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
505 h->gob_number = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
506 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
507 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
508
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
509 static int h261_decode_gob(H261Context *h){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
510 MpegEncContext * const s = &h->s;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
511 int i;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
512
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
513 ff_set_qscale(s, s->qscale);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
514 while(h->current_mba <= MAX_MBA)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
515 {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
516 int ret;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
517 /* DCT & quantize */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
518 s->dsp.clear_blocks(s->block[0]);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
519 ret= h261_decode_mb(h, s->block);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
520 if(ret<0){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
521 const int xy= s->mb_x + s->mb_y*s->mb_stride;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
522 if(ret==SLICE_END){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
523 MPV_decode_mb(s, s->block);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
524 if(h->loop_filter){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
525 ff_h261_loop_filter(h);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
526 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
527 h->loop_filter = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
528 for(i=1; i<h->mba_diff; i++){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
529 s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1-i) % 11);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
530 s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1-i) / 11);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
531 ff_init_block_index(s);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
532 ff_update_block_index(s);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
533 s->dsp.clear_blocks(s->block[0]);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
534 ret= h261_decode_mb_skipped(h, s->block);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
535 MPV_decode_mb(s, s->block);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
536 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
537
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
538 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
539 }else if(ret==SLICE_NOEND){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
540 av_log(s->avctx, AV_LOG_ERROR, "Slice mismatch at MB: %d\n", xy);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
541 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
542 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
543 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
544 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
545 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
546 MPV_decode_mb(s, s->block);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
547 if(h->loop_filter){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
548 ff_h261_loop_filter(h);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
549 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
550
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
551 h->loop_filter = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
552 for(i=1; i<h->mba_diff; i++){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
553 s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1-i) % 11);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
554 s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1-i) / 11);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
555 ff_init_block_index(s);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
556 ff_update_block_index(s);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
557 s->dsp.clear_blocks(s->block[0]);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
558 ret= h261_decode_mb_skipped(h, s->block);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
559 MPV_decode_mb(s, s->block);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
560 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
561 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
562
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
563 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
564 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
565
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
566 static int h261_find_frame_end(ParseContext *pc, AVCodecContext* avctx, const uint8_t *buf, int buf_size){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
567 int vop_found, i, j, bits_left, last_bits;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
568 uint32_t state;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
569
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
570 H261Context *h = avctx->priv_data;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
571
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
572 if(h){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
573 bits_left = h->bits_left;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
574 last_bits = h->last_bits;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
575 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
576 else{
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
577 bits_left = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
578 last_bits = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
579 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
580
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
581 vop_found= pc->frame_start_found;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
582 state= pc->state;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
583 if(bits_left!=0 && !vop_found)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
584 state = state << (8-bits_left) | last_bits;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
585 i=0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
586 if(!vop_found){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
587 for(i=0; i<buf_size; i++){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
588 state= (state<<8) | buf[i];
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
589 for(j=0; j<8; j++){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
590 if(( ( (state<<j) | (buf[i]>>(8-j)) )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
591 i++;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
592 vop_found=1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
593 break;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
594 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
595 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
596 if(vop_found)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
597 break;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
598 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
599 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
600 if(vop_found){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
601 for(; i<buf_size; i++){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
602 if(avctx->flags & CODEC_FLAG_TRUNCATED)//XXX ffplay workaround, someone a better solution?
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
603 state= (state<<8) | buf[i];
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
604 for(j=0; j<8; j++){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
605 if(( ( (state<<j) | (buf[i]>>(8-j)) )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
606 pc->frame_start_found=0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
607 pc->state=-1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
608 return i-3;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
609 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
610 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
611 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
612 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
613
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
614 pc->frame_start_found= vop_found;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
615 pc->state= state;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
616 return END_NOT_FOUND;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
617 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
618
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
619 static int h261_parse(AVCodecParserContext *s,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
620 AVCodecContext *avctx,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
621 uint8_t **poutbuf, int *poutbuf_size,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
622 const uint8_t *buf, int buf_size)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
623 {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
624 ParseContext *pc = s->priv_data;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
625 int next;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
626
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
627 next= h261_find_frame_end(pc,avctx, buf, buf_size);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
628 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
629 *poutbuf = NULL;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
630 *poutbuf_size = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
631 return buf_size;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
632 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
633 *poutbuf = (uint8_t *)buf;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
634 *poutbuf_size = buf_size;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
635 return next;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
636 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
637
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
638 /**
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
639 * returns the number of bytes consumed for building the current frame
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
640 */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
641 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
642 int pos= (get_bits_count(&s->gb)+7)>>3;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
643
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
644 if(s->flags&CODEC_FLAG_TRUNCATED){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
645 pos -= s->parse_context.last_index;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
646 if(pos<0) pos=0;// padding is not really read so this might be -1
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
647 return pos;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
648 }else{
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
649 if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
650 if(pos+10>buf_size) pos=buf_size; // oops ;)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
651
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
652 return pos;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
653 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
654 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
655
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
656 static int h261_decode_frame(AVCodecContext *avctx,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
657 void *data, int *data_size,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
658 uint8_t *buf, int buf_size)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
659 {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
660 H261Context *h= avctx->priv_data;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
661 MpegEncContext *s = &h->s;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
662 int ret;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
663 AVFrame *pict = data;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
664
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
665 #ifdef DEBUG
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
666 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
667 printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
668 #endif
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
669 s->flags= avctx->flags;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
670 s->flags2= avctx->flags2;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
671
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
672 /* no supplementary picture */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
673 if (buf_size == 0) {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
674
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
675 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
676 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
677
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
678 if(s->flags&CODEC_FLAG_TRUNCATED){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
679 int next;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
680
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
681 next= h261_find_frame_end(&s->parse_context,avctx, buf, buf_size);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
682
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
683 if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
684 return buf_size;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
685 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
686
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
687
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
688 retry:
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
689
2047
541686b38a5e removing h263 specific code
michael
parents: 2046
diff changeset
690 init_get_bits(&s->gb, buf, buf_size*8);
2044
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
691
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
692 if(!s->context_initialized){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
693 if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
694 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
695 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
696
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
697 //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
698 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
699 int i= ff_find_unused_picture(s, 0);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
700 s->current_picture_ptr= &s->picture[i];
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
701 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
702
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
703 ret = h261_decode_picture_header(h);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
704
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
705 /* skip if the header was thrashed */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
706 if (ret < 0){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
707 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
708 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
709 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
710
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
711 if (s->width != avctx->width || s->height != avctx->height){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
712 ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
713 s->parse_context.buffer=0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
714 MPV_common_end(s);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
715 s->parse_context= pc;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
716 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
717 if (!s->context_initialized) {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
718 avctx->width = s->width;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
719 avctx->height = s->height;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
720
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
721 goto retry;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
722 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
723
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
724 // for hurry_up==5
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
725 s->current_picture.pict_type= s->pict_type;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
726 s->current_picture.key_frame= s->pict_type == I_TYPE;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
727
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
728 /* skip everything if we are in a hurry>=5 */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
729 if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
730
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
731 if(MPV_frame_start(s, avctx) < 0)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
732 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
733
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
734 ff_er_frame_start(s);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
735
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
736 /* decode each macroblock */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
737 s->mb_x=0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
738 s->mb_y=0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
739
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
740 while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
741 if(ff_h261_resync(h)<0)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
742 break;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
743 h261_decode_gob(h);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
744 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
745 MPV_frame_end(s);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
746
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
747 // h261 doesn't have byte aligned codes
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
748 // store the bits of the next frame that are left in the last byte
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
749 // in the H261Context and remember the number of stored bits
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
750 {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
751 int bitsleft;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
752 int current_pos= get_bits_count(&s->gb)>>3;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
753 bitsleft = (current_pos<<3) - get_bits_count(&s->gb);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
754 h->bits_left = - bitsleft;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
755 if(bitsleft > 0)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
756 h->last_bits= get_bits(&s->gb, 8 - h->bits_left);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
757 else
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
758 h->last_bits = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
759 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
760
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
761 assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
762 assert(s->current_picture.pict_type == s->pict_type);
2047
541686b38a5e removing h263 specific code
michael
parents: 2046
diff changeset
763 *pict= *(AVFrame*)&s->current_picture;
541686b38a5e removing h263 specific code
michael
parents: 2046
diff changeset
764 ff_print_debug_info(s, pict);
2044
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
765
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
766 /* Return the Picture timestamp as the frame number */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
767 /* we substract 1 because it is added on utils.c */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
768 avctx->frame_number = s->picture_number - 1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
769
2047
541686b38a5e removing h263 specific code
michael
parents: 2046
diff changeset
770 *data_size = sizeof(AVFrame);
2044
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
771
2047
541686b38a5e removing h263 specific code
michael
parents: 2046
diff changeset
772 return get_consumed_bytes(s, buf_size);
2044
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
773 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
774
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
775 static int h261_decode_end(AVCodecContext *avctx)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
776 {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
777 H261Context *h= avctx->priv_data;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
778 MpegEncContext *s = &h->s;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
779
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
780 MPV_common_end(s);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
781 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
782 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
783
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
784 AVCodec h261_decoder = {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
785 "h261",
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
786 CODEC_TYPE_VIDEO,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
787 CODEC_ID_H261,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
788 sizeof(H261Context),
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
789 h261_decode_init,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
790 NULL,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
791 h261_decode_end,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
792 h261_decode_frame,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
793 CODEC_CAP_TRUNCATED,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
794 };
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
795
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
796 AVCodecParser h261_parser = {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
797 { CODEC_ID_H261 },
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
798 sizeof(ParseContext),
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
799 NULL,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
800 h261_parse,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
801 ff_parse_close,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
802 };