2
0

mpeg12.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. * @file
  24. * MPEG-1/2 decoder
  25. */
  26. #define UNCHECKED_BITSTREAM_READER 1
  27. #include "libavutil/attributes.h"
  28. #include "libavutil/avassert.h"
  29. #include "libavutil/timecode.h"
  30. #include "internal.h"
  31. #include "avcodec.h"
  32. #include "mpegvideo.h"
  33. #include "error_resilience.h"
  34. #include "mpeg12.h"
  35. #include "mpeg12data.h"
  36. #include "mpegvideodata.h"
  37. #include "bytestream.h"
  38. #include "thread.h"
  39. uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
  40. static const uint8_t table_mb_ptype[7][2] = {
  41. { 3, 5 }, // 0x01 MB_INTRA
  42. { 1, 2 }, // 0x02 MB_PAT
  43. { 1, 3 }, // 0x08 MB_FOR
  44. { 1, 1 }, // 0x0A MB_FOR|MB_PAT
  45. { 1, 6 }, // 0x11 MB_QUANT|MB_INTRA
  46. { 1, 5 }, // 0x12 MB_QUANT|MB_PAT
  47. { 2, 5 }, // 0x1A MB_QUANT|MB_FOR|MB_PAT
  48. };
  49. static const uint8_t table_mb_btype[11][2] = {
  50. { 3, 5 }, // 0x01 MB_INTRA
  51. { 2, 3 }, // 0x04 MB_BACK
  52. { 3, 3 }, // 0x06 MB_BACK|MB_PAT
  53. { 2, 4 }, // 0x08 MB_FOR
  54. { 3, 4 }, // 0x0A MB_FOR|MB_PAT
  55. { 2, 2 }, // 0x0C MB_FOR|MB_BACK
  56. { 3, 2 }, // 0x0E MB_FOR|MB_BACK|MB_PAT
  57. { 1, 6 }, // 0x11 MB_QUANT|MB_INTRA
  58. { 2, 6 }, // 0x16 MB_QUANT|MB_BACK|MB_PAT
  59. { 3, 6 }, // 0x1A MB_QUANT|MB_FOR|MB_PAT
  60. { 2, 5 }, // 0x1E MB_QUANT|MB_FOR|MB_BACK|MB_PAT
  61. };
  62. av_cold void ff_init_2d_vlc_rl(RLTable *rl, unsigned static_size, int flags)
  63. {
  64. int i;
  65. VLC_TYPE table[680][2] = {{0}};
  66. VLC vlc = { .table = table, .table_allocated = static_size };
  67. av_assert0(static_size <= FF_ARRAY_ELEMS(table));
  68. init_vlc(&vlc, TEX_VLC_BITS, rl->n + 2, &rl->table_vlc[0][1], 4, 2, &rl->table_vlc[0][0], 4, 2, INIT_VLC_USE_NEW_STATIC | flags);
  69. for (i = 0; i < vlc.table_size; i++) {
  70. int code = vlc.table[i][0];
  71. int len = vlc.table[i][1];
  72. int level, run;
  73. if (len == 0) { // illegal code
  74. run = 65;
  75. level = MAX_LEVEL;
  76. } else if (len<0) { //more bits needed
  77. run = 0;
  78. level = code;
  79. } else {
  80. if (code == rl->n) { //esc
  81. run = 65;
  82. level = 0;
  83. } else if (code == rl->n+1) { //eob
  84. run = 0;
  85. level = 127;
  86. } else {
  87. run = rl->table_run [code] + 1;
  88. level = rl->table_level[code];
  89. }
  90. }
  91. rl->rl_vlc[0][i].len = len;
  92. rl->rl_vlc[0][i].level = level;
  93. rl->rl_vlc[0][i].run = run;
  94. }
  95. }
  96. av_cold void ff_mpeg12_common_init(MpegEncContext *s)
  97. {
  98. s->y_dc_scale_table =
  99. s->c_dc_scale_table = ff_mpeg2_dc_scale_table[s->intra_dc_precision];
  100. }
  101. void ff_mpeg1_clean_buffers(MpegEncContext *s)
  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. /* decoding */
  110. VLC ff_mv_vlc;
  111. VLC ff_dc_lum_vlc;
  112. VLC ff_dc_chroma_vlc;
  113. VLC ff_mbincr_vlc;
  114. VLC ff_mb_ptype_vlc;
  115. VLC ff_mb_btype_vlc;
  116. VLC ff_mb_pat_vlc;
  117. av_cold void ff_mpeg12_init_vlcs(void)
  118. {
  119. static int done = 0;
  120. if (!done) {
  121. done = 1;
  122. INIT_VLC_STATIC(&ff_dc_lum_vlc, DC_VLC_BITS, 12,
  123. ff_mpeg12_vlc_dc_lum_bits, 1, 1,
  124. ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
  125. INIT_VLC_STATIC(&ff_dc_chroma_vlc, DC_VLC_BITS, 12,
  126. ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
  127. ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
  128. INIT_VLC_STATIC(&ff_mv_vlc, MV_VLC_BITS, 17,
  129. &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
  130. &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
  131. INIT_VLC_STATIC(&ff_mbincr_vlc, MBINCR_VLC_BITS, 36,
  132. &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
  133. &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
  134. INIT_VLC_STATIC(&ff_mb_pat_vlc, MB_PAT_VLC_BITS, 64,
  135. &ff_mpeg12_mbPatTable[0][1], 2, 1,
  136. &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
  137. INIT_VLC_STATIC(&ff_mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
  138. &table_mb_ptype[0][1], 2, 1,
  139. &table_mb_ptype[0][0], 2, 1, 64);
  140. INIT_VLC_STATIC(&ff_mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
  141. &table_mb_btype[0][1], 2, 1,
  142. &table_mb_btype[0][0], 2, 1, 64);
  143. ff_rl_init(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
  144. ff_rl_init(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
  145. INIT_2D_VLC_RL(ff_rl_mpeg1, 680, 0);
  146. INIT_2D_VLC_RL(ff_rl_mpeg2, 674, 0);
  147. }
  148. }
  149. /**
  150. * Find the end of the current frame in the bitstream.
  151. * @return the position of the first byte of the next frame, or -1
  152. */
  153. int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
  154. {
  155. int i;
  156. uint32_t state = pc->state;
  157. /* EOF considered as end of frame */
  158. if (buf_size == 0)
  159. return 0;
  160. /*
  161. 0 frame start -> 1/4
  162. 1 first_SEQEXT -> 0/2
  163. 2 first field start -> 3/0
  164. 3 second_SEQEXT -> 2/0
  165. 4 searching end
  166. */
  167. for (i = 0; i < buf_size; i++) {
  168. av_assert1(pc->frame_start_found >= 0 && pc->frame_start_found <= 4);
  169. if (pc->frame_start_found & 1) {
  170. if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80)
  171. pc->frame_start_found--;
  172. else if (state == EXT_START_CODE + 2) {
  173. if ((buf[i] & 3) == 3)
  174. pc->frame_start_found = 0;
  175. else
  176. pc->frame_start_found = (pc->frame_start_found + 1) & 3;
  177. }
  178. state++;
  179. } else {
  180. i = avpriv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
  181. if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
  182. i++;
  183. pc->frame_start_found = 4;
  184. }
  185. if (state == SEQ_END_CODE) {
  186. pc->frame_start_found = 0;
  187. pc->state=-1;
  188. return i+1;
  189. }
  190. if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
  191. pc->frame_start_found = 0;
  192. if (pc->frame_start_found < 4 && state == EXT_START_CODE)
  193. pc->frame_start_found++;
  194. if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
  195. if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) {
  196. pc->frame_start_found = 0;
  197. pc->state = -1;
  198. return i - 3;
  199. }
  200. }
  201. if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
  202. ff_fetch_timestamp(s, i - 3, 1, i > 3);
  203. }
  204. }
  205. }
  206. pc->state = state;
  207. return END_NOT_FOUND;
  208. }
  209. #define MAX_INDEX (64 - 1)
  210. int ff_mpeg1_decode_block_intra(GetBitContext *gb,
  211. const uint16_t *quant_matrix,
  212. uint8_t *const scantable, int last_dc[3],
  213. int16_t *block, int index, int qscale)
  214. {
  215. int dc, diff, i = 0, component;
  216. RLTable *rl = &ff_rl_mpeg1;
  217. /* DC coefficient */
  218. component = index <= 3 ? 0 : index - 4 + 1;
  219. diff = decode_dc(gb, component);
  220. if (diff >= 0xffff)
  221. return AVERROR_INVALIDDATA;
  222. dc = last_dc[component];
  223. dc += diff;
  224. last_dc[component] = dc;
  225. block[0] = dc * quant_matrix[0];
  226. {
  227. OPEN_READER(re, gb);
  228. UPDATE_CACHE(re, gb);
  229. if (((int32_t)GET_CACHE(re, gb)) <= (int32_t)0xBFFFFFFF)
  230. goto end;
  231. /* now quantify & encode AC coefficients */
  232. while (1) {
  233. int level, run, j;
  234. GET_RL_VLC(level, run, re, gb, rl->rl_vlc[0],
  235. TEX_VLC_BITS, 2, 0);
  236. if (level != 0) {
  237. i += run;
  238. if (i > MAX_INDEX)
  239. break;
  240. j = scantable[i];
  241. level = (level * qscale * quant_matrix[j]) >> 4;
  242. level = (level - 1) | 1;
  243. level = (level ^ SHOW_SBITS(re, gb, 1)) -
  244. SHOW_SBITS(re, gb, 1);
  245. SKIP_BITS(re, gb, 1);
  246. } else {
  247. /* escape */
  248. run = SHOW_UBITS(re, gb, 6) + 1;
  249. LAST_SKIP_BITS(re, gb, 6);
  250. UPDATE_CACHE(re, gb);
  251. level = SHOW_SBITS(re, gb, 8);
  252. SKIP_BITS(re, gb, 8);
  253. if (level == -128) {
  254. level = SHOW_UBITS(re, gb, 8) - 256;
  255. SKIP_BITS(re, gb, 8);
  256. } else if (level == 0) {
  257. level = SHOW_UBITS(re, gb, 8);
  258. SKIP_BITS(re, gb, 8);
  259. }
  260. i += run;
  261. if (i > MAX_INDEX)
  262. break;
  263. j = scantable[i];
  264. if (level < 0) {
  265. level = -level;
  266. level = (level * qscale * quant_matrix[j]) >> 4;
  267. level = (level - 1) | 1;
  268. level = -level;
  269. } else {
  270. level = (level * qscale * quant_matrix[j]) >> 4;
  271. level = (level - 1) | 1;
  272. }
  273. }
  274. block[j] = level;
  275. if (((int32_t)GET_CACHE(re, gb)) <= (int32_t)0xBFFFFFFF)
  276. break;
  277. UPDATE_CACHE(re, gb);
  278. }
  279. end:
  280. LAST_SKIP_BITS(re, gb, 2);
  281. CLOSE_READER(re, gb);
  282. }
  283. if (i > MAX_INDEX)
  284. i = AVERROR_INVALIDDATA;
  285. return i;
  286. }