]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpeg12.c
Merge commit 'ffba2053edfc177d217bf4a95edf51cd0fc40753'
[ffmpeg] / libavcodec / mpeg12.c
1 /*
2  * MPEG-1/2 decoder
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * MPEG-1/2 decoder
26  */
27
28 #define UNCHECKED_BITSTREAM_READER 1
29
30 //#define DEBUG
31 #include "libavutil/attributes.h"
32 #include "libavutil/avassert.h"
33 #include "libavutil/timecode.h"
34
35 #include "internal.h"
36 #include "avcodec.h"
37 #include "dsputil.h"
38 #include "mpegvideo.h"
39 #include "error_resilience.h"
40 #include "mpeg12.h"
41 #include "mpeg12data.h"
42 #include "mpeg12decdata.h"
43 #include "bytestream.h"
44 #include "vdpau_internal.h"
45 #include "xvmc_internal.h"
46 #include "thread.h"
47
48
49 uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
50
51 #define INIT_2D_VLC_RL(rl, static_size)\
52 {\
53     static RL_VLC_ELEM rl_vlc_table[static_size];\
54     INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
55                     &rl.table_vlc[0][1], 4, 2,\
56                     &rl.table_vlc[0][0], 4, 2, static_size);\
57 \
58     rl.rl_vlc[0] = rl_vlc_table;\
59     init_2d_vlc_rl(&rl);\
60 }
61
62 static av_cold void init_2d_vlc_rl(RLTable *rl)
63 {
64     int i;
65
66     for (i = 0; i < rl->vlc.table_size; i++) {
67         int code = rl->vlc.table[i][0];
68         int len  = rl->vlc.table[i][1];
69         int level, run;
70
71         if (len == 0) { // illegal code
72             run   = 65;
73             level = MAX_LEVEL;
74         } else if (len<0) { //more bits needed
75             run   = 0;
76             level = code;
77         } else {
78             if (code == rl->n) { //esc
79                 run   = 65;
80                 level = 0;
81             } else if (code == rl->n+1) { //eob
82                 run   = 0;
83                 level = 127;
84             } else {
85                 run   = rl->table_run  [code] + 1;
86                 level = rl->table_level[code];
87             }
88         }
89         rl->rl_vlc[0][i].len   = len;
90         rl->rl_vlc[0][i].level = level;
91         rl->rl_vlc[0][i].run   = run;
92     }
93 }
94
95 av_cold void ff_mpeg12_common_init(MpegEncContext *s)
96 {
97
98     s->y_dc_scale_table =
99     s->c_dc_scale_table = ff_mpeg2_dc_scale_table[s->intra_dc_precision];
100
101 }
102
103 void ff_mpeg1_clean_buffers(MpegEncContext *s)
104 {
105     s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
106     s->last_dc[1] = s->last_dc[0];
107     s->last_dc[2] = s->last_dc[0];
108     memset(s->last_mv, 0, sizeof(s->last_mv));
109 }
110
111
112 /******************************************/
113 /* decoding */
114
115 VLC ff_mv_vlc;
116
117 VLC ff_dc_lum_vlc;
118 VLC ff_dc_chroma_vlc;
119
120 VLC ff_mbincr_vlc;
121 VLC ff_mb_ptype_vlc;
122 VLC ff_mb_btype_vlc;
123 VLC ff_mb_pat_vlc;
124
125 av_cold void ff_mpeg12_init_vlcs(void)
126 {
127     static int done = 0;
128
129     if (!done) {
130         done = 1;
131
132         INIT_VLC_STATIC(&ff_dc_lum_vlc, DC_VLC_BITS, 12,
133                         ff_mpeg12_vlc_dc_lum_bits, 1, 1,
134                         ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
135         INIT_VLC_STATIC(&ff_dc_chroma_vlc,  DC_VLC_BITS, 12,
136                         ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
137                         ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
138         INIT_VLC_STATIC(&ff_mv_vlc, MV_VLC_BITS, 17,
139                         &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
140                         &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
141         INIT_VLC_STATIC(&ff_mbincr_vlc, MBINCR_VLC_BITS, 36,
142                         &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
143                         &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
144         INIT_VLC_STATIC(&ff_mb_pat_vlc, MB_PAT_VLC_BITS, 64,
145                         &ff_mpeg12_mbPatTable[0][1], 2, 1,
146                         &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
147
148         INIT_VLC_STATIC(&ff_mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
149                         &table_mb_ptype[0][1], 2, 1,
150                         &table_mb_ptype[0][0], 2, 1, 64);
151         INIT_VLC_STATIC(&ff_mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
152                         &table_mb_btype[0][1], 2, 1,
153                         &table_mb_btype[0][0], 2, 1, 64);
154         ff_init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
155         ff_init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
156
157         INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
158         INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
159     }
160 }
161
162 /**
163  * Find the end of the current frame in the bitstream.
164  * @return the position of the first byte of the next frame, or -1
165  */
166 int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
167 {
168     int i;
169     uint32_t state = pc->state;
170
171     /* EOF considered as end of frame */
172     if (buf_size == 0)
173         return 0;
174
175 /*
176  0  frame start         -> 1/4
177  1  first_SEQEXT        -> 0/2
178  2  first field start   -> 3/0
179  3  second_SEQEXT       -> 2/0
180  4  searching end
181 */
182
183     for (i = 0; i < buf_size; i++) {
184         av_assert1(pc->frame_start_found >= 0 && pc->frame_start_found <= 4);
185         if (pc->frame_start_found & 1) {
186             if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80)
187                 pc->frame_start_found--;
188             else if (state == EXT_START_CODE + 2) {
189                 if ((buf[i] & 3) == 3)
190                     pc->frame_start_found = 0;
191                 else
192                     pc->frame_start_found = (pc->frame_start_found + 1) & 3;
193             }
194             state++;
195         } else {
196             i = avpriv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
197             if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
198                 i++;
199                 pc->frame_start_found = 4;
200             }
201             if (state == SEQ_END_CODE) {
202                 pc->frame_start_found = 0;
203                 pc->state=-1;
204                 return i+1;
205             }
206             if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
207                 pc->frame_start_found = 0;
208             if (pc->frame_start_found  < 4 && state == EXT_START_CODE)
209                 pc->frame_start_found++;
210             if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
211                 if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) {
212                     pc->frame_start_found = 0;
213                     pc->state             = -1;
214                     return i - 3;
215                 }
216             }
217             if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
218                 ff_fetch_timestamp(s, i - 3, 1);
219             }
220         }
221     }
222     pc->state = state;
223     return END_NOT_FOUND;
224 }
225