annotate h261.c @ 2044:b6f2add2511e libavcodec

h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
author michael
date Sun, 30 May 2004 20:37:15 +0000
parents
children 9447bbd8a7e9
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 int i;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
62 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
63 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
64 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
65 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
66 uint8_t *dest_cr= s->dest[2];
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
67 uint8_t *src;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
68
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
69 CHECKED_ALLOCZ((src),sizeof(uint8_t) * 64 );
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
70
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
71 for(i=0; i<8;i++)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
72 memcpy(src+i*8,dest_y+i*linesize,sizeof(uint8_t) * 8 );
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
73 s->dsp.h261_v_loop_filter(dest_y, src, linesize);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
74 s->dsp.h261_h_loop_filter(dest_y, src, linesize);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
75
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
76 for(i=0; i<8;i++)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
77 memcpy(src+i*8,dest_y+i*linesize + 8,sizeof(uint8_t) * 8 );
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
78 s->dsp.h261_v_loop_filter(dest_y + 8, src, linesize);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
79 s->dsp.h261_h_loop_filter(dest_y + 8, src, linesize);
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 for(i=0; i<8;i++)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
82 memcpy(src+i*8,dest_y+(i+8)*linesize,sizeof(uint8_t) * 8 );
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
83 s->dsp.h261_v_loop_filter(dest_y + 8 * linesize, src, linesize);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
84 s->dsp.h261_h_loop_filter(dest_y + 8 * linesize, src, linesize);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
85
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
86 for(i=0; i<8;i++)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
87 memcpy(src+i*8,dest_y+(i+8)*linesize + 8,sizeof(uint8_t) * 8 );
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
88 s->dsp.h261_v_loop_filter(dest_y + 8 * linesize + 8, src, linesize);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
89 s->dsp.h261_h_loop_filter(dest_y + 8 * linesize + 8, src, linesize);
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 for(i=0; i<8;i++)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
92 memcpy(src+i*8,dest_cb+i*uvlinesize,sizeof(uint8_t) * 8 );
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
93 s->dsp.h261_v_loop_filter(dest_cb, src, uvlinesize);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
94 s->dsp.h261_h_loop_filter(dest_cb, src, uvlinesize);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
95
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
96 for(i=0; i<8;i++)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
97 memcpy(src+i*8,dest_cr+i*uvlinesize,sizeof(uint8_t) * 8 );
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
98 s->dsp.h261_v_loop_filter(dest_cr, src, uvlinesize);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
99 s->dsp.h261_h_loop_filter(dest_cr, src, uvlinesize);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
100
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
101 fail:
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
102 av_free(src);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
103
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
104 return;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
105 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
106
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
107 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
108 int n, int coded);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
109 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
110 DCTELEM block[6][64]);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
111 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
112
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
113 /***********************************************/
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
114 /* decoding */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
115
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
116 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
117 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
118 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
119 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
120
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
121 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
122
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
123 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
124 static int done = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
125
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
126 if(!done){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
127 done = 1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
128 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
129 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
130 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
131 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
132 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
133 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
134 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
135 &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
136 &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
137 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
138 &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
139 &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
140 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
141 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
142 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
143 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
144
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
145 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
146 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
147 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
148
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
149 // set defaults
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
150 MPV_decode_defaults(s);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
151 s->avctx = avctx;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
152
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
153 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
154 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
155 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
156
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
157 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
158 s->low_delay= 1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
159 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
160
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
161 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
162
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
163 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
164
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
165 h->bits_left = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
166 h->last_bits = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
167
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
168 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
169 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
170
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 * 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
173 * @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
174 */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
175 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
176 unsigned int val;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
177 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
178
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
179 /* 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
180 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
181 if(val)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
182 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
183
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
184 /* We have a GBSC */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
185 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
186
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
187 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
188 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
189
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
190 /* GEI */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
191 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
192 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
193 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
194
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
195 if(s->qscale==0)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
196 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
197
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
198 // 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
199 // 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
200 // 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
201 h->current_mba = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
202 h->mba_diff = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
203
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
204 return 0;
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
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 * 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
209 * @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
210 */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
211 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
212 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
213 int left, ret;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
214
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
215 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
216 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
217 if(ret>=0)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
218 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
219 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
220 //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
221 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
222 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
223 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
224
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
225 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
226 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
227 GetBitContext bak= s->gb;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
228
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
229 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
230 if(ret>=0)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
231 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
232
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
233 s->gb= bak;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
234 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
235 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
236 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
237
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
238 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
239 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
240
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
241 /**
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
242 * 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
243 * @return 0
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
244 */
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_skipped(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;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
249 int i;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
250 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
251 s->mb_intra = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
252 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
253 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
254 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
255 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
256 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
257 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
258 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
259 s->mb_skiped = 1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
260 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
261 }
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 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
264 DCTELEM block[6][64])
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 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
267 int i, cbp, mv_x_diff, mv_y_diff, sign, xy;
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 cbp = 63;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
270 // Read mba
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
271 do{
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
272 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
273 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
274 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
275
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
276 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
277 return -1;
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 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
280
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
281 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
282 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
283
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
284 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
285 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
286
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
287 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
288
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
289 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
290 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
291
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
292 // Read mtype
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
293 int old_mtype = h->mtype;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
294 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
295 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
296
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
297 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
298 h->loop_filter = 1;
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 // Read mquant
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
301 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
302 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
303 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
304
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
305 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
306
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
307 // Read mv
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
308 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
309 // 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
310 // 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
311 // following three situations:
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
312 // 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
313 // 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
314 // 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
315 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
316 ( 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
317 {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
318 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
319 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
320 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
321
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
322 mv_x_diff = get_vlc2(&s->gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
323 mv_x_diff = mvmap[mv_x_diff];
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
324
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
325 if(mv_x_diff != 0){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
326 sign = get_bits1(&s->gb);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
327
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
328 if(!sign)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
329 mv_x_diff= -mv_x_diff;
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
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
332 mv_y_diff = get_vlc2(&s->gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
333 mv_y_diff = mvmap[mv_y_diff];
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
334
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
335 if(mv_y_diff != 0){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
336 sign = get_bits1(&s->gb);
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 if(!sign)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
339 mv_y_diff= -mv_y_diff;
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 //mv's are in the range -15...15
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
343 if((h->current_mv_x + mv_x_diff > -16) && (h->current_mv_x + mv_x_diff < 16) )
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
344 h->current_mv_x += mv_x_diff;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
345 else
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
346 h->current_mv_x += (mv_x_diff + (mv_x_diff > 0 ? -32 : 32));
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
347
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
348 if((h->current_mv_y + mv_y_diff > -16) && (h->current_mv_y + mv_y_diff < 16) )
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
349 h->current_mv_y += mv_y_diff;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
350 else
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
351 h->current_mv_y += (mv_y_diff + (mv_y_diff>0 ? -32 : 32));
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 // Read cbp
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
355 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
356 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
357 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
358
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
359 if(s->mb_intra){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
360 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
361 goto intra;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
362 }
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 //set motion vectors
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
365 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
366 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
367 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
368 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
369 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
370 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
371 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
372 else{
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
373 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
374 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
375 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
376
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
377 intra:
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
378 /* decode each block */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
379 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
380 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
381 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
382 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
383 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
384 cbp+=cbp;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
385 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
386 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
387
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
388 /* 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
389 {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
390 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
391
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
392 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
393 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
394 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
395
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
396 if(v==0){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
397 return SLICE_END;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
398 }
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 return SLICE_OK;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
401 }
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 /**
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
404 * decodes a macroblock
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
405 * @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
406 */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
407 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
408 int n, int coded)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
409 {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
410 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
411 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
412 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
413 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
414
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
415 // 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
416 // 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
417 // 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
418 // coded with 8 bits.
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
419 // 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
420 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
421 if (s->mb_intra){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
422 /* DC coef */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
423 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
424 // 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
425 if((level&0x7F) == 0){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
426 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
427 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
428 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
429 // 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
430 if (level == 255)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
431 level = 128;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
432 block[0] = level;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
433 i = 1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
434 }else if(coded){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
435 // Run Level Code
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
436 // 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
437 // 0 1 1s
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
438 // * * 0*
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
439 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
440 i = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
441 if ( check & 0x2 ){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
442 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
443 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
444 i = 1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
445 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
446 }else{
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
447 i = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
448 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
449 if(!coded){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
450 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
451 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
452 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
453 for(;;){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
454 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
455 if (code < 0){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
456 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
457 return -1;
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 (code == rl->n) {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
460 /* escape */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
461 // 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
462 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
463 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
464 }else if(code == 0){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
465 break;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
466 }else{
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
467 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
468 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
469 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
470 level = -level;
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 i += run;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
473 if (i >= 64){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
474 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
475 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
476 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
477 j = scan_table[i];
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
478 block[j] = level;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
479 i++;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
480 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
481 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
482 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
483 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
484
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 * 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
487 * @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
488 */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
489 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
490 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
491 int format, i;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
492 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
493 uint32_t startcode;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
494 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
495
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
496 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
497
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
498 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
499 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
500
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
501 if(startcode == 0x10)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
502 break;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
503 }
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 if (startcode != 0x10){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
506 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
507 return -1;
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
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
510 /* temporal reference */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
511 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
512
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
513 /* PTYPE starts here */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
514 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
515 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
516 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
517
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
518 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
519
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
520 //only 2 formats possible
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
521 if (format == 0){//QCIF
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
522 s->width = 176;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
523 s->height = 144;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
524 s->mb_width = 11;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
525 s->mb_height = 9;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
526 }else{//CIF
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
527 s->width = 352;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
528 s->height = 288;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
529 s->mb_width = 22;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
530 s->mb_height = 18;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
531 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
532
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
533 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
534
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
535 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
536 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
537
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
538 /* PEI */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
539 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
540 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
541 }
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 //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
544 if(h261_framecounter > 1)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
545 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
546 else
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
547 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
548
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
549 h261_framecounter++;
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->gob_number = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
552 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
553 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
554
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
555 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
556 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
557 int i;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
558
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
559 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
560 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
561 {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
562 int ret;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
563 /* DCT & quantize */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
564 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
565 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
566 if(ret<0){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
567 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
568 if(ret==SLICE_END){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
569 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
570 if(h->loop_filter){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
571 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
572 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
573 h->loop_filter = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
574 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
575 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
576 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
577 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
578 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
579 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
580 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
581 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
582 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
583
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
584 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
585 }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
586 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
587 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
588 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
589 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
590 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
591 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
592 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
593 if(h->loop_filter){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
594 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
595 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
596
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
597 h->loop_filter = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
598 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
599 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
600 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
601 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
602 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
603 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
604 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
605 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
606 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
607 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
608
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
609 return -1;
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 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
613 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
614 uint32_t state;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
615
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
616 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
617
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
618 if(h){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
619 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
620 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
621 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
622 else{
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
623 bits_left = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
624 last_bits = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
625 }
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 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
628 state= pc->state;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
629 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
630 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
631 i=0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
632 if(!vop_found){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
633 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
634 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
635 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
636 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
637 i++;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
638 vop_found=1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
639 break;
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 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
642 if(vop_found)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
643 break;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
644 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
645 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
646 if(vop_found){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
647 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
648 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
649 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
650 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
651 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
652 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
653 pc->state=-1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
654 return i-3;
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 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
657 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
658 }
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 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
661 pc->state= state;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
662 return END_NOT_FOUND;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
663 }
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 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
666 AVCodecContext *avctx,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
667 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
668 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
669 {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
670 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
671 int next;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
672
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
673 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
674 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
675 *poutbuf = NULL;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
676 *poutbuf_size = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
677 return buf_size;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
678 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
679 *poutbuf = (uint8_t *)buf;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
680 *poutbuf_size = buf_size;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
681 return next;
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
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
684 /**
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
685 * 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
686 */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
687 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
688 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
689
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
690 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
691 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
692 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
693 return pos;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
694 }else{
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
695 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
696 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
697
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
698 return pos;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
699 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
700 }
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 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
703 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
704 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
705 {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
706 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
707 MpegEncContext *s = &h->s;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
708 int ret;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
709 AVFrame *pict = data;
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 #ifdef PRINT_FRAME_TIME
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
712 uint64_t time= rdtsc();
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
713 #endif
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
714 #ifdef DEBUG
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
715 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
716 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
717 #endif
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
718 s->flags= avctx->flags;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
719 s->flags2= avctx->flags2;
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 /* no supplementary picture */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
722 if (buf_size == 0) {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
723 /* special case for last picture */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
724 if (s->low_delay==0 && s->next_picture_ptr) {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
725 *pict= *(AVFrame*)s->next_picture_ptr;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
726 s->next_picture_ptr= NULL;
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 *data_size = sizeof(AVFrame);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
729 }
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 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
732 }
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 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
735 int next;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
736
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
737 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
738
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
739 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
740 return buf_size;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
741 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
742
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
743
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
744 retry:
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
745
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
746 if(s->bitstream_buffer_size && buf_size<20){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
747 init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
748 }else
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
749 init_get_bits(&s->gb, buf, buf_size*8);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
750 s->bitstream_buffer_size=0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
751
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
752 if(!s->context_initialized){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
753 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
754 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
755 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
756
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
757 //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
758 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
759 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
760 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
761 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
762
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
763 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
764
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
765 if(ret==FRAME_SKIPED) 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
766
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
767 /* 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
768 if (ret < 0){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
769 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
770 return -1;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
771 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
772
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
773 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
774 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
775 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
776 MPV_common_end(s);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
777 s->parse_context= pc;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
778 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
779 if (!s->context_initialized) {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
780 avctx->width = s->width;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
781 avctx->height = s->height;
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 goto retry;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
784 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
785
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
786 // for hurry_up==5
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
787 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
788 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
789
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
790 /* 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
791 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
792
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
793 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
794 return -1;
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 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
797
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
798 /* decode each macroblock */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
799 s->mb_x=0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
800 s->mb_y=0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
801
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
802 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
803 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
804 break;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
805 h261_decode_gob(h);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
806 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
807 MPV_frame_end(s);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
808
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
809 // 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
810 // 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
811 // 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
812 {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
813 int bitsleft;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
814 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
815 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
816 h->bits_left = - bitsleft;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
817 if(bitsleft > 0)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
818 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
819 else
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
820 h->last_bits = 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
821 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
822
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
823 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
824 assert(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
825 if(s->low_delay){
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
826 *pict= *(AVFrame*)&s->current_picture;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
827 ff_print_debug_info(s, pict);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
828 }else{
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
829 *pict= *(AVFrame*)&s->last_picture;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
830 if(pict)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
831 ff_print_debug_info(s, pict);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
832 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
833
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
834 /* 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
835 /* 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
836 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
837
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
838 /* dont output the last pic after seeking */
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
839 if(s->last_picture_ptr || s->low_delay)
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
840 *data_size = sizeof(AVFrame);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
841 #ifdef PRINT_FRAME_TIME
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
842 printf("%Ld\n", rdtsc()-time);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
843 #endif
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
844
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
845 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
846 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
847
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
848 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
849 {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
850 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
851 MpegEncContext *s = &h->s;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
852
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
853 MPV_common_end(s);
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
854 return 0;
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
855 }
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
856
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
857 AVCodec h261_decoder = {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
858 "h261",
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
859 CODEC_TYPE_VIDEO,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
860 CODEC_ID_H261,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
861 sizeof(H261Context),
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
862 h261_decode_init,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
863 NULL,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
864 h261_decode_end,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
865 h261_decode_frame,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
866 CODEC_CAP_TRUNCATED,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
867 };
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
868
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
869 AVCodecParser h261_parser = {
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
870 { CODEC_ID_H261 },
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
871 sizeof(ParseContext),
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
872 NULL,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
873 h261_parse,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
874 ff_parse_close,
b6f2add2511e h261 decoder by (Maarten Daniels <maarten.daniels at student dot luc dot ac dot be>)
michael
parents:
diff changeset
875 };