FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mpeg12.c
Go to the documentation of this file.
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 #include "libavutil/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/timecode.h"
33 
34 #include "internal.h"
35 #include "avcodec.h"
36 #include "dsputil.h"
37 #include "mpegvideo.h"
38 #include "error_resilience.h"
39 #include "mpeg12.h"
40 #include "mpeg12data.h"
41 #include "mpeg12decdata.h"
42 #include "bytestream.h"
43 #include "vdpau_internal.h"
44 #include "xvmc_internal.h"
45 #include "thread.h"
46 
48 
49 #define INIT_2D_VLC_RL(rl, static_size)\
50 {\
51  static RL_VLC_ELEM rl_vlc_table[static_size];\
52  INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
53  &rl.table_vlc[0][1], 4, 2,\
54  &rl.table_vlc[0][0], 4, 2, static_size);\
55 \
56  rl.rl_vlc[0] = rl_vlc_table;\
57  init_2d_vlc_rl(&rl);\
58 }
59 
60 static av_cold void init_2d_vlc_rl(RLTable *rl)
61 {
62  int i;
63 
64  for (i = 0; i < rl->vlc.table_size; i++) {
65  int code = rl->vlc.table[i][0];
66  int len = rl->vlc.table[i][1];
67  int level, run;
68 
69  if (len == 0) { // illegal code
70  run = 65;
71  level = MAX_LEVEL;
72  } else if (len<0) { //more bits needed
73  run = 0;
74  level = code;
75  } else {
76  if (code == rl->n) { //esc
77  run = 65;
78  level = 0;
79  } else if (code == rl->n+1) { //eob
80  run = 0;
81  level = 127;
82  } else {
83  run = rl->table_run [code] + 1;
84  level = rl->table_level[code];
85  }
86  }
87  rl->rl_vlc[0][i].len = len;
88  rl->rl_vlc[0][i].level = level;
89  rl->rl_vlc[0][i].run = run;
90  }
91 }
92 
94 {
95 
96  s->y_dc_scale_table =
98 
99 }
100 
102 {
103  s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
104  s->last_dc[1] = s->last_dc[0];
105  s->last_dc[2] = s->last_dc[0];
106  memset(s->last_mv, 0, sizeof(s->last_mv));
107 }
108 
109 
110 /******************************************/
111 /* decoding */
112 
114 
117 
122 
124 {
125  static int done = 0;
126 
127  if (!done) {
128  done = 1;
129 
130  INIT_VLC_STATIC(&ff_dc_lum_vlc, DC_VLC_BITS, 12,
132  ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
133  INIT_VLC_STATIC(&ff_dc_chroma_vlc, DC_VLC_BITS, 12,
135  ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
136  INIT_VLC_STATIC(&ff_mv_vlc, MV_VLC_BITS, 17,
137  &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
138  &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
139  INIT_VLC_STATIC(&ff_mbincr_vlc, MBINCR_VLC_BITS, 36,
140  &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
141  &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
142  INIT_VLC_STATIC(&ff_mb_pat_vlc, MB_PAT_VLC_BITS, 64,
143  &ff_mpeg12_mbPatTable[0][1], 2, 1,
144  &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
145 
146  INIT_VLC_STATIC(&ff_mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
147  &table_mb_ptype[0][1], 2, 1,
148  &table_mb_ptype[0][0], 2, 1, 64);
149  INIT_VLC_STATIC(&ff_mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
150  &table_mb_btype[0][1], 2, 1,
151  &table_mb_btype[0][0], 2, 1, 64);
154 
157  }
158 }
159 
160 /**
161  * Find the end of the current frame in the bitstream.
162  * @return the position of the first byte of the next frame, or -1
163  */
165 {
166  int i;
167  uint32_t state = pc->state;
168 
169  /* EOF considered as end of frame */
170  if (buf_size == 0)
171  return 0;
172 
173 /*
174  0 frame start -> 1/4
175  1 first_SEQEXT -> 0/2
176  2 first field start -> 3/0
177  3 second_SEQEXT -> 2/0
178  4 searching end
179 */
180 
181  for (i = 0; i < buf_size; i++) {
182  av_assert1(pc->frame_start_found >= 0 && pc->frame_start_found <= 4);
183  if (pc->frame_start_found & 1) {
184  if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80)
185  pc->frame_start_found--;
186  else if (state == EXT_START_CODE + 2) {
187  if ((buf[i] & 3) == 3)
188  pc->frame_start_found = 0;
189  else
190  pc->frame_start_found = (pc->frame_start_found + 1) & 3;
191  }
192  state++;
193  } else {
194  i = avpriv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
195  if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
196  i++;
197  pc->frame_start_found = 4;
198  }
199  if (state == SEQ_END_CODE) {
200  pc->frame_start_found = 0;
201  pc->state=-1;
202  return i+1;
203  }
204  if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
205  pc->frame_start_found = 0;
206  if (pc->frame_start_found < 4 && state == EXT_START_CODE)
207  pc->frame_start_found++;
208  if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
209  if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) {
210  pc->frame_start_found = 0;
211  pc->state = -1;
212  return i - 3;
213  }
214  }
215  if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
216  ff_fetch_timestamp(s, i - 3, 1);
217  }
218  }
219  }
220  pc->state = state;
221  return END_NOT_FOUND;
222 }
223