You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2693 lines
96KB

  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. //#define DEBUG
  28. #include "internal.h"
  29. #include "avcodec.h"
  30. #include "dsputil.h"
  31. #include "mpegvideo.h"
  32. #include "libavutil/avassert.h"
  33. #include "libavutil/timecode.h"
  34. #include "mpeg12.h"
  35. #include "mpeg12data.h"
  36. #include "mpeg12decdata.h"
  37. #include "bytestream.h"
  38. #include "vdpau_internal.h"
  39. #include "xvmc_internal.h"
  40. #include "thread.h"
  41. #define MV_VLC_BITS 9
  42. #define MBINCR_VLC_BITS 9
  43. #define MB_PAT_VLC_BITS 9
  44. #define MB_PTYPE_VLC_BITS 6
  45. #define MB_BTYPE_VLC_BITS 6
  46. static VLC mv_vlc;
  47. /* as H.263, but only 17 codes */
  48. static int mpeg_decode_motion(MpegEncContext *s, int fcode, int pred)
  49. {
  50. int code, sign, val, shift;
  51. code = get_vlc2(&s->gb, mv_vlc.table, MV_VLC_BITS, 2);
  52. if (code == 0) {
  53. return pred;
  54. }
  55. if (code < 0) {
  56. return 0xffff;
  57. }
  58. sign = get_bits1(&s->gb);
  59. shift = fcode - 1;
  60. val = code;
  61. if (shift) {
  62. val = (val - 1) << shift;
  63. val |= get_bits(&s->gb, shift);
  64. val++;
  65. }
  66. if (sign)
  67. val = -val;
  68. val += pred;
  69. /* modulo decoding */
  70. return sign_extend(val, 5 + shift);
  71. }
  72. static inline int mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
  73. {
  74. int level, dc, diff, i, j, run;
  75. int component;
  76. RLTable *rl = &ff_rl_mpeg1;
  77. uint8_t * const scantable = s->intra_scantable.permutated;
  78. const uint16_t *quant_matrix = s->intra_matrix;
  79. const int qscale = s->qscale;
  80. /* DC coefficient */
  81. component = (n <= 3 ? 0 : n - 4 + 1);
  82. diff = decode_dc(&s->gb, component);
  83. if (diff >= 0xffff)
  84. return -1;
  85. dc = s->last_dc[component];
  86. dc += diff;
  87. s->last_dc[component] = dc;
  88. block[0] = dc * quant_matrix[0];
  89. av_dlog(s->avctx, "dc=%d diff=%d\n", dc, diff);
  90. i = 0;
  91. {
  92. OPEN_READER(re, &s->gb);
  93. /* now quantify & encode AC coefficients */
  94. for (;;) {
  95. UPDATE_CACHE(re, &s->gb);
  96. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  97. if (level == 127) {
  98. break;
  99. } else if (level != 0) {
  100. i += run;
  101. j = scantable[i];
  102. level = (level * qscale * quant_matrix[j]) >> 4;
  103. level = (level - 1) | 1;
  104. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  105. LAST_SKIP_BITS(re, &s->gb, 1);
  106. } else {
  107. /* escape */
  108. run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
  109. UPDATE_CACHE(re, &s->gb);
  110. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  111. if (level == -128) {
  112. level = SHOW_UBITS(re, &s->gb, 8) - 256; LAST_SKIP_BITS(re, &s->gb, 8);
  113. } else if (level == 0) {
  114. level = SHOW_UBITS(re, &s->gb, 8) ; LAST_SKIP_BITS(re, &s->gb, 8);
  115. }
  116. i += run;
  117. j = scantable[i];
  118. if (level < 0) {
  119. level = -level;
  120. level = (level * qscale * quant_matrix[j]) >> 4;
  121. level = (level - 1) | 1;
  122. level = -level;
  123. } else {
  124. level = (level * qscale * quant_matrix[j]) >> 4;
  125. level = (level - 1) | 1;
  126. }
  127. }
  128. if (i > 63) {
  129. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  130. return -1;
  131. }
  132. block[j] = level;
  133. }
  134. CLOSE_READER(re, &s->gb);
  135. }
  136. s->block_last_index[n] = i;
  137. return 0;
  138. }
  139. int ff_mpeg1_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
  140. {
  141. return mpeg1_decode_block_intra(s, block, n);
  142. }
  143. static inline int mpeg1_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
  144. {
  145. int level, i, j, run;
  146. RLTable *rl = &ff_rl_mpeg1;
  147. uint8_t * const scantable = s->intra_scantable.permutated;
  148. const uint16_t *quant_matrix = s->inter_matrix;
  149. const int qscale = s->qscale;
  150. {
  151. OPEN_READER(re, &s->gb);
  152. i = -1;
  153. // special case for first coefficient, no need to add second VLC table
  154. UPDATE_CACHE(re, &s->gb);
  155. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  156. level = (3 * qscale * quant_matrix[0]) >> 5;
  157. level = (level - 1) | 1;
  158. if (GET_CACHE(re, &s->gb) & 0x40000000)
  159. level = -level;
  160. block[0] = level;
  161. i++;
  162. SKIP_BITS(re, &s->gb, 2);
  163. if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  164. goto end;
  165. }
  166. /* now quantify & encode AC coefficients */
  167. for (;;) {
  168. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  169. if (level != 0) {
  170. i += run;
  171. j = scantable[i];
  172. level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
  173. level = (level - 1) | 1;
  174. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  175. SKIP_BITS(re, &s->gb, 1);
  176. } else {
  177. /* escape */
  178. run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
  179. UPDATE_CACHE(re, &s->gb);
  180. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  181. if (level == -128) {
  182. level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
  183. } else if (level == 0) {
  184. level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
  185. }
  186. i += run;
  187. j = scantable[i];
  188. if (level < 0) {
  189. level = -level;
  190. level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
  191. level = (level - 1) | 1;
  192. level = -level;
  193. } else {
  194. level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
  195. level = (level - 1) | 1;
  196. }
  197. }
  198. if (i > 63) {
  199. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  200. return -1;
  201. }
  202. block[j] = level;
  203. if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  204. break;
  205. UPDATE_CACHE(re, &s->gb);
  206. }
  207. end:
  208. LAST_SKIP_BITS(re, &s->gb, 2);
  209. CLOSE_READER(re, &s->gb);
  210. }
  211. s->block_last_index[n] = i;
  212. return 0;
  213. }
  214. static inline int mpeg1_fast_decode_block_inter(MpegEncContext *s, DCTELEM *block, int n)
  215. {
  216. int level, i, j, run;
  217. RLTable *rl = &ff_rl_mpeg1;
  218. uint8_t * const scantable = s->intra_scantable.permutated;
  219. const int qscale = s->qscale;
  220. {
  221. OPEN_READER(re, &s->gb);
  222. i = -1;
  223. // special case for first coefficient, no need to add second VLC table
  224. UPDATE_CACHE(re, &s->gb);
  225. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  226. level = (3 * qscale) >> 1;
  227. level = (level - 1) | 1;
  228. if (GET_CACHE(re, &s->gb) & 0x40000000)
  229. level = -level;
  230. block[0] = level;
  231. i++;
  232. SKIP_BITS(re, &s->gb, 2);
  233. if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  234. goto end;
  235. }
  236. /* now quantify & encode AC coefficients */
  237. for (;;) {
  238. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  239. if (level != 0) {
  240. i += run;
  241. j = scantable[i];
  242. level = ((level * 2 + 1) * qscale) >> 1;
  243. level = (level - 1) | 1;
  244. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  245. SKIP_BITS(re, &s->gb, 1);
  246. } else {
  247. /* escape */
  248. run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
  249. UPDATE_CACHE(re, &s->gb);
  250. level = SHOW_SBITS(re, &s->gb, 8); SKIP_BITS(re, &s->gb, 8);
  251. if (level == -128) {
  252. level = SHOW_UBITS(re, &s->gb, 8) - 256; SKIP_BITS(re, &s->gb, 8);
  253. } else if (level == 0) {
  254. level = SHOW_UBITS(re, &s->gb, 8) ; SKIP_BITS(re, &s->gb, 8);
  255. }
  256. i += run;
  257. j = scantable[i];
  258. if (level < 0) {
  259. level = -level;
  260. level = ((level * 2 + 1) * qscale) >> 1;
  261. level = (level - 1) | 1;
  262. level = -level;
  263. } else {
  264. level = ((level * 2 + 1) * qscale) >> 1;
  265. level = (level - 1) | 1;
  266. }
  267. }
  268. block[j] = level;
  269. if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  270. break;
  271. UPDATE_CACHE(re, &s->gb);
  272. }
  273. end:
  274. LAST_SKIP_BITS(re, &s->gb, 2);
  275. CLOSE_READER(re, &s->gb);
  276. }
  277. s->block_last_index[n] = i;
  278. return 0;
  279. }
  280. static inline int mpeg2_decode_block_non_intra(MpegEncContext *s, DCTELEM *block, int n)
  281. {
  282. int level, i, j, run;
  283. RLTable *rl = &ff_rl_mpeg1;
  284. uint8_t * const scantable = s->intra_scantable.permutated;
  285. const uint16_t *quant_matrix;
  286. const int qscale = s->qscale;
  287. int mismatch;
  288. mismatch = 1;
  289. {
  290. OPEN_READER(re, &s->gb);
  291. i = -1;
  292. if (n < 4)
  293. quant_matrix = s->inter_matrix;
  294. else
  295. quant_matrix = s->chroma_inter_matrix;
  296. // special case for first coefficient, no need to add second VLC table
  297. UPDATE_CACHE(re, &s->gb);
  298. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  299. level= (3 * qscale * quant_matrix[0]) >> 5;
  300. if (GET_CACHE(re, &s->gb) & 0x40000000)
  301. level = -level;
  302. block[0] = level;
  303. mismatch ^= level;
  304. i++;
  305. SKIP_BITS(re, &s->gb, 2);
  306. if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  307. goto end;
  308. }
  309. /* now quantify & encode AC coefficients */
  310. for (;;) {
  311. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  312. if (level != 0) {
  313. i += run;
  314. j = scantable[i];
  315. level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
  316. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  317. SKIP_BITS(re, &s->gb, 1);
  318. } else {
  319. /* escape */
  320. run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
  321. UPDATE_CACHE(re, &s->gb);
  322. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  323. i += run;
  324. j = scantable[i];
  325. if (level < 0) {
  326. level = ((-level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
  327. level = -level;
  328. } else {
  329. level = ((level * 2 + 1) * qscale * quant_matrix[j]) >> 5;
  330. }
  331. }
  332. if (i > 63) {
  333. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  334. return -1;
  335. }
  336. mismatch ^= level;
  337. block[j] = level;
  338. if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  339. break;
  340. UPDATE_CACHE(re, &s->gb);
  341. }
  342. end:
  343. LAST_SKIP_BITS(re, &s->gb, 2);
  344. CLOSE_READER(re, &s->gb);
  345. }
  346. block[63] ^= (mismatch & 1);
  347. s->block_last_index[n] = i;
  348. return 0;
  349. }
  350. static inline int mpeg2_fast_decode_block_non_intra(MpegEncContext *s,
  351. DCTELEM *block, int n)
  352. {
  353. int level, i, j, run;
  354. RLTable *rl = &ff_rl_mpeg1;
  355. uint8_t * const scantable = s->intra_scantable.permutated;
  356. const int qscale = s->qscale;
  357. OPEN_READER(re, &s->gb);
  358. i = -1;
  359. // special case for first coefficient, no need to add second VLC table
  360. UPDATE_CACHE(re, &s->gb);
  361. if (((int32_t)GET_CACHE(re, &s->gb)) < 0) {
  362. level = (3 * qscale) >> 1;
  363. if (GET_CACHE(re, &s->gb) & 0x40000000)
  364. level = -level;
  365. block[0] = level;
  366. i++;
  367. SKIP_BITS(re, &s->gb, 2);
  368. if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  369. goto end;
  370. }
  371. /* now quantify & encode AC coefficients */
  372. for (;;) {
  373. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  374. if (level != 0) {
  375. i += run;
  376. j = scantable[i];
  377. level = ((level * 2 + 1) * qscale) >> 1;
  378. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  379. SKIP_BITS(re, &s->gb, 1);
  380. } else {
  381. /* escape */
  382. run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
  383. UPDATE_CACHE(re, &s->gb);
  384. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  385. i += run;
  386. j = scantable[i];
  387. if (level < 0) {
  388. level = ((-level * 2 + 1) * qscale) >> 1;
  389. level = -level;
  390. } else {
  391. level = ((level * 2 + 1) * qscale) >> 1;
  392. }
  393. }
  394. block[j] = level;
  395. if (((int32_t)GET_CACHE(re, &s->gb)) <= (int32_t)0xBFFFFFFF)
  396. break;
  397. UPDATE_CACHE(re, &s->gb);
  398. }
  399. end:
  400. LAST_SKIP_BITS(re, &s->gb, 2);
  401. CLOSE_READER(re, &s->gb);
  402. s->block_last_index[n] = i;
  403. return 0;
  404. }
  405. static inline int mpeg2_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
  406. {
  407. int level, dc, diff, i, j, run;
  408. int component;
  409. RLTable *rl;
  410. uint8_t * const scantable = s->intra_scantable.permutated;
  411. const uint16_t *quant_matrix;
  412. const int qscale = s->qscale;
  413. int mismatch;
  414. /* DC coefficient */
  415. if (n < 4) {
  416. quant_matrix = s->intra_matrix;
  417. component = 0;
  418. } else {
  419. quant_matrix = s->chroma_intra_matrix;
  420. component = (n & 1) + 1;
  421. }
  422. diff = decode_dc(&s->gb, component);
  423. if (diff >= 0xffff)
  424. return -1;
  425. dc = s->last_dc[component];
  426. dc += diff;
  427. s->last_dc[component] = dc;
  428. block[0] = dc << (3 - s->intra_dc_precision);
  429. av_dlog(s->avctx, "dc=%d\n", block[0]);
  430. mismatch = block[0] ^ 1;
  431. i = 0;
  432. if (s->intra_vlc_format)
  433. rl = &ff_rl_mpeg2;
  434. else
  435. rl = &ff_rl_mpeg1;
  436. {
  437. OPEN_READER(re, &s->gb);
  438. /* now quantify & encode AC coefficients */
  439. for (;;) {
  440. UPDATE_CACHE(re, &s->gb);
  441. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  442. if (level == 127) {
  443. break;
  444. } else if (level != 0) {
  445. i += run;
  446. j = scantable[i];
  447. level = (level * qscale * quant_matrix[j]) >> 4;
  448. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  449. LAST_SKIP_BITS(re, &s->gb, 1);
  450. } else {
  451. /* escape */
  452. run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
  453. UPDATE_CACHE(re, &s->gb);
  454. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  455. i += run;
  456. j = scantable[i];
  457. if (level < 0) {
  458. level = (-level * qscale * quant_matrix[j]) >> 4;
  459. level = -level;
  460. } else {
  461. level = (level * qscale * quant_matrix[j]) >> 4;
  462. }
  463. }
  464. if (i > 63) {
  465. av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
  466. return -1;
  467. }
  468. mismatch ^= level;
  469. block[j] = level;
  470. }
  471. CLOSE_READER(re, &s->gb);
  472. }
  473. block[63] ^= mismatch & 1;
  474. s->block_last_index[n] = i;
  475. return 0;
  476. }
  477. static inline int mpeg2_fast_decode_block_intra(MpegEncContext *s, DCTELEM *block, int n)
  478. {
  479. int level, dc, diff, j, run;
  480. int component;
  481. RLTable *rl;
  482. uint8_t * scantable = s->intra_scantable.permutated;
  483. const uint16_t *quant_matrix;
  484. const int qscale = s->qscale;
  485. /* DC coefficient */
  486. if (n < 4) {
  487. quant_matrix = s->intra_matrix;
  488. component = 0;
  489. } else {
  490. quant_matrix = s->chroma_intra_matrix;
  491. component = (n & 1) + 1;
  492. }
  493. diff = decode_dc(&s->gb, component);
  494. if (diff >= 0xffff)
  495. return -1;
  496. dc = s->last_dc[component];
  497. dc += diff;
  498. s->last_dc[component] = dc;
  499. block[0] = dc << (3 - s->intra_dc_precision);
  500. if (s->intra_vlc_format)
  501. rl = &ff_rl_mpeg2;
  502. else
  503. rl = &ff_rl_mpeg1;
  504. {
  505. OPEN_READER(re, &s->gb);
  506. /* now quantify & encode AC coefficients */
  507. for (;;) {
  508. UPDATE_CACHE(re, &s->gb);
  509. GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
  510. if (level == 127) {
  511. break;
  512. } else if (level != 0) {
  513. scantable += run;
  514. j = *scantable;
  515. level = (level * qscale * quant_matrix[j]) >> 4;
  516. level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
  517. LAST_SKIP_BITS(re, &s->gb, 1);
  518. } else {
  519. /* escape */
  520. run = SHOW_UBITS(re, &s->gb, 6) + 1; LAST_SKIP_BITS(re, &s->gb, 6);
  521. UPDATE_CACHE(re, &s->gb);
  522. level = SHOW_SBITS(re, &s->gb, 12); SKIP_BITS(re, &s->gb, 12);
  523. scantable += run;
  524. j = *scantable;
  525. if (level < 0) {
  526. level = (-level * qscale * quant_matrix[j]) >> 4;
  527. level = -level;
  528. } else {
  529. level = (level * qscale * quant_matrix[j]) >> 4;
  530. }
  531. }
  532. block[j] = level;
  533. }
  534. CLOSE_READER(re, &s->gb);
  535. }
  536. s->block_last_index[n] = scantable - s->intra_scantable.permutated;
  537. return 0;
  538. }
  539. uint8_t ff_mpeg12_static_rl_table_store[2][2][2*MAX_RUN + MAX_LEVEL + 3];
  540. #define INIT_2D_VLC_RL(rl, static_size)\
  541. {\
  542. static RL_VLC_ELEM rl_vlc_table[static_size];\
  543. INIT_VLC_STATIC(&rl.vlc, TEX_VLC_BITS, rl.n + 2,\
  544. &rl.table_vlc[0][1], 4, 2,\
  545. &rl.table_vlc[0][0], 4, 2, static_size);\
  546. \
  547. rl.rl_vlc[0] = rl_vlc_table;\
  548. init_2d_vlc_rl(&rl);\
  549. }
  550. static void init_2d_vlc_rl(RLTable *rl)
  551. {
  552. int i;
  553. for (i = 0; i < rl->vlc.table_size; i++) {
  554. int code = rl->vlc.table[i][0];
  555. int len = rl->vlc.table[i][1];
  556. int level, run;
  557. if (len == 0) { // illegal code
  558. run = 65;
  559. level = MAX_LEVEL;
  560. } else if (len<0) { //more bits needed
  561. run = 0;
  562. level = code;
  563. } else {
  564. if (code == rl->n) { //esc
  565. run = 65;
  566. level = 0;
  567. } else if (code == rl->n+1) { //eob
  568. run = 0;
  569. level = 127;
  570. } else {
  571. run = rl->table_run [code] + 1;
  572. level = rl->table_level[code];
  573. }
  574. }
  575. rl->rl_vlc[0][i].len = len;
  576. rl->rl_vlc[0][i].level = level;
  577. rl->rl_vlc[0][i].run = run;
  578. }
  579. }
  580. void ff_mpeg12_common_init(MpegEncContext *s)
  581. {
  582. s->y_dc_scale_table =
  583. s->c_dc_scale_table = ff_mpeg2_dc_scale_table[s->intra_dc_precision];
  584. }
  585. void ff_mpeg1_clean_buffers(MpegEncContext *s)
  586. {
  587. s->last_dc[0] = 1 << (7 + s->intra_dc_precision);
  588. s->last_dc[1] = s->last_dc[0];
  589. s->last_dc[2] = s->last_dc[0];
  590. memset(s->last_mv, 0, sizeof(s->last_mv));
  591. }
  592. /******************************************/
  593. /* decoding */
  594. VLC ff_dc_lum_vlc;
  595. VLC ff_dc_chroma_vlc;
  596. static VLC mbincr_vlc;
  597. static VLC mb_ptype_vlc;
  598. static VLC mb_btype_vlc;
  599. static VLC mb_pat_vlc;
  600. av_cold void ff_mpeg12_init_vlcs(void)
  601. {
  602. static int done = 0;
  603. if (!done) {
  604. done = 1;
  605. INIT_VLC_STATIC(&ff_dc_lum_vlc, DC_VLC_BITS, 12,
  606. ff_mpeg12_vlc_dc_lum_bits, 1, 1,
  607. ff_mpeg12_vlc_dc_lum_code, 2, 2, 512);
  608. INIT_VLC_STATIC(&ff_dc_chroma_vlc, DC_VLC_BITS, 12,
  609. ff_mpeg12_vlc_dc_chroma_bits, 1, 1,
  610. ff_mpeg12_vlc_dc_chroma_code, 2, 2, 514);
  611. INIT_VLC_STATIC(&mv_vlc, MV_VLC_BITS, 17,
  612. &ff_mpeg12_mbMotionVectorTable[0][1], 2, 1,
  613. &ff_mpeg12_mbMotionVectorTable[0][0], 2, 1, 518);
  614. INIT_VLC_STATIC(&mbincr_vlc, MBINCR_VLC_BITS, 36,
  615. &ff_mpeg12_mbAddrIncrTable[0][1], 2, 1,
  616. &ff_mpeg12_mbAddrIncrTable[0][0], 2, 1, 538);
  617. INIT_VLC_STATIC(&mb_pat_vlc, MB_PAT_VLC_BITS, 64,
  618. &ff_mpeg12_mbPatTable[0][1], 2, 1,
  619. &ff_mpeg12_mbPatTable[0][0], 2, 1, 512);
  620. INIT_VLC_STATIC(&mb_ptype_vlc, MB_PTYPE_VLC_BITS, 7,
  621. &table_mb_ptype[0][1], 2, 1,
  622. &table_mb_ptype[0][0], 2, 1, 64);
  623. INIT_VLC_STATIC(&mb_btype_vlc, MB_BTYPE_VLC_BITS, 11,
  624. &table_mb_btype[0][1], 2, 1,
  625. &table_mb_btype[0][0], 2, 1, 64);
  626. ff_init_rl(&ff_rl_mpeg1, ff_mpeg12_static_rl_table_store[0]);
  627. ff_init_rl(&ff_rl_mpeg2, ff_mpeg12_static_rl_table_store[1]);
  628. INIT_2D_VLC_RL(ff_rl_mpeg1, 680);
  629. INIT_2D_VLC_RL(ff_rl_mpeg2, 674);
  630. }
  631. }
  632. static inline int get_dmv(MpegEncContext *s)
  633. {
  634. if (get_bits1(&s->gb))
  635. return 1 - (get_bits1(&s->gb) << 1);
  636. else
  637. return 0;
  638. }
  639. static inline int get_qscale(MpegEncContext *s)
  640. {
  641. int qscale = get_bits(&s->gb, 5);
  642. if (s->q_scale_type) {
  643. return non_linear_qscale[qscale];
  644. } else {
  645. return qscale << 1;
  646. }
  647. }
  648. static void exchange_uv(MpegEncContext *s)
  649. {
  650. DCTELEM (*tmp)[64];
  651. tmp = s->pblocks[4];
  652. s->pblocks[4] = s->pblocks[5];
  653. s->pblocks[5] = tmp;
  654. }
  655. /* motion type (for MPEG-2) */
  656. #define MT_FIELD 1
  657. #define MT_FRAME 2
  658. #define MT_16X8 2
  659. #define MT_DMV 3
  660. static int mpeg_decode_mb(MpegEncContext *s, DCTELEM block[12][64])
  661. {
  662. int i, j, k, cbp, val, mb_type, motion_type;
  663. const int mb_block_count = 4 + (1 << s->chroma_format);
  664. av_dlog(s->avctx, "decode_mb: x=%d y=%d\n", s->mb_x, s->mb_y);
  665. av_assert2(s->mb_skipped == 0);
  666. if (s->mb_skip_run-- != 0) {
  667. if (s->pict_type == AV_PICTURE_TYPE_P) {
  668. s->mb_skipped = 1;
  669. s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] = MB_TYPE_SKIP | MB_TYPE_L0 | MB_TYPE_16x16;
  670. } else {
  671. int mb_type;
  672. if (s->mb_x)
  673. mb_type = s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1];
  674. else
  675. mb_type = s->current_picture.f.mb_type[s->mb_width + (s->mb_y - 1) * s->mb_stride - 1]; // FIXME not sure if this is allowed in MPEG at all
  676. if (IS_INTRA(mb_type))
  677. return -1;
  678. s->current_picture.f.mb_type[s->mb_x + s->mb_y*s->mb_stride] =
  679. mb_type | MB_TYPE_SKIP;
  680. // av_assert2(s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride - 1] & (MB_TYPE_16x16 | MB_TYPE_16x8));
  681. if ((s->mv[0][0][0] | s->mv[0][0][1] | s->mv[1][0][0] | s->mv[1][0][1]) == 0)
  682. s->mb_skipped = 1;
  683. }
  684. return 0;
  685. }
  686. switch (s->pict_type) {
  687. default:
  688. case AV_PICTURE_TYPE_I:
  689. if (get_bits1(&s->gb) == 0) {
  690. if (get_bits1(&s->gb) == 0) {
  691. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in I Frame at %d %d\n", s->mb_x, s->mb_y);
  692. return -1;
  693. }
  694. mb_type = MB_TYPE_QUANT | MB_TYPE_INTRA;
  695. } else {
  696. mb_type = MB_TYPE_INTRA;
  697. }
  698. break;
  699. case AV_PICTURE_TYPE_P:
  700. mb_type = get_vlc2(&s->gb, mb_ptype_vlc.table, MB_PTYPE_VLC_BITS, 1);
  701. if (mb_type < 0) {
  702. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in P Frame at %d %d\n", s->mb_x, s->mb_y);
  703. return -1;
  704. }
  705. mb_type = ptype2mb_type[mb_type];
  706. break;
  707. case AV_PICTURE_TYPE_B:
  708. mb_type = get_vlc2(&s->gb, mb_btype_vlc.table, MB_BTYPE_VLC_BITS, 1);
  709. if (mb_type < 0) {
  710. av_log(s->avctx, AV_LOG_ERROR, "invalid mb type in B Frame at %d %d\n", s->mb_x, s->mb_y);
  711. return -1;
  712. }
  713. mb_type = btype2mb_type[mb_type];
  714. break;
  715. }
  716. av_dlog(s->avctx, "mb_type=%x\n", mb_type);
  717. // motion_type = 0; /* avoid warning */
  718. if (IS_INTRA(mb_type)) {
  719. s->dsp.clear_blocks(s->block[0]);
  720. if (!s->chroma_y_shift) {
  721. s->dsp.clear_blocks(s->block[6]);
  722. }
  723. /* compute DCT type */
  724. if (s->picture_structure == PICT_FRAME && // FIXME add an interlaced_dct coded var?
  725. !s->frame_pred_frame_dct) {
  726. s->interlaced_dct = get_bits1(&s->gb);
  727. }
  728. if (IS_QUANT(mb_type))
  729. s->qscale = get_qscale(s);
  730. if (s->concealment_motion_vectors) {
  731. /* just parse them */
  732. if (s->picture_structure != PICT_FRAME)
  733. skip_bits1(&s->gb); /* field select */
  734. s->mv[0][0][0]= s->last_mv[0][0][0]= s->last_mv[0][1][0] =
  735. mpeg_decode_motion(s, s->mpeg_f_code[0][0], s->last_mv[0][0][0]);
  736. s->mv[0][0][1]= s->last_mv[0][0][1]= s->last_mv[0][1][1] =
  737. mpeg_decode_motion(s, s->mpeg_f_code[0][1], s->last_mv[0][0][1]);
  738. skip_bits1(&s->gb); /* marker */
  739. } else
  740. memset(s->last_mv, 0, sizeof(s->last_mv)); /* reset mv prediction */
  741. s->mb_intra = 1;
  742. // if 1, we memcpy blocks in xvmcvideo
  743. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
  744. ff_xvmc_pack_pblocks(s, -1); // inter are always full blocks
  745. if (s->swap_uv) {
  746. exchange_uv(s);
  747. }
  748. }
  749. if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
  750. if (s->flags2 & CODEC_FLAG2_FAST) {
  751. for (i = 0; i < 6; i++) {
  752. mpeg2_fast_decode_block_intra(s, *s->pblocks[i], i);
  753. }
  754. } else {
  755. for (i = 0; i < mb_block_count; i++) {
  756. if (mpeg2_decode_block_intra(s, *s->pblocks[i], i) < 0)
  757. return -1;
  758. }
  759. }
  760. } else {
  761. for (i = 0; i < 6; i++) {
  762. if (mpeg1_decode_block_intra(s, *s->pblocks[i], i) < 0)
  763. return -1;
  764. }
  765. }
  766. } else {
  767. if (mb_type & MB_TYPE_ZERO_MV) {
  768. av_assert2(mb_type & MB_TYPE_CBP);
  769. s->mv_dir = MV_DIR_FORWARD;
  770. if (s->picture_structure == PICT_FRAME) {
  771. if (!s->frame_pred_frame_dct)
  772. s->interlaced_dct = get_bits1(&s->gb);
  773. s->mv_type = MV_TYPE_16X16;
  774. } else {
  775. s->mv_type = MV_TYPE_FIELD;
  776. mb_type |= MB_TYPE_INTERLACED;
  777. s->field_select[0][0] = s->picture_structure - 1;
  778. }
  779. if (IS_QUANT(mb_type))
  780. s->qscale = get_qscale(s);
  781. s->last_mv[0][0][0] = 0;
  782. s->last_mv[0][0][1] = 0;
  783. s->last_mv[0][1][0] = 0;
  784. s->last_mv[0][1][1] = 0;
  785. s->mv[0][0][0] = 0;
  786. s->mv[0][0][1] = 0;
  787. } else {
  788. av_assert2(mb_type & MB_TYPE_L0L1);
  789. // FIXME decide if MBs in field pictures are MB_TYPE_INTERLACED
  790. /* get additional motion vector type */
  791. if (s->frame_pred_frame_dct)
  792. motion_type = MT_FRAME;
  793. else {
  794. motion_type = get_bits(&s->gb, 2);
  795. if (s->picture_structure == PICT_FRAME && HAS_CBP(mb_type))
  796. s->interlaced_dct = get_bits1(&s->gb);
  797. }
  798. if (IS_QUANT(mb_type))
  799. s->qscale = get_qscale(s);
  800. /* motion vectors */
  801. s->mv_dir = (mb_type >> 13) & 3;
  802. av_dlog(s->avctx, "motion_type=%d\n", motion_type);
  803. switch (motion_type) {
  804. case MT_FRAME: /* or MT_16X8 */
  805. if (s->picture_structure == PICT_FRAME) {
  806. mb_type |= MB_TYPE_16x16;
  807. s->mv_type = MV_TYPE_16X16;
  808. for (i = 0; i < 2; i++) {
  809. if (USES_LIST(mb_type, i)) {
  810. /* MT_FRAME */
  811. s->mv[i][0][0]= s->last_mv[i][0][0]= s->last_mv[i][1][0] =
  812. mpeg_decode_motion(s, s->mpeg_f_code[i][0], s->last_mv[i][0][0]);
  813. s->mv[i][0][1]= s->last_mv[i][0][1]= s->last_mv[i][1][1] =
  814. mpeg_decode_motion(s, s->mpeg_f_code[i][1], s->last_mv[i][0][1]);
  815. /* full_pel: only for MPEG-1 */
  816. if (s->full_pel[i]) {
  817. s->mv[i][0][0] <<= 1;
  818. s->mv[i][0][1] <<= 1;
  819. }
  820. }
  821. }
  822. } else {
  823. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  824. s->mv_type = MV_TYPE_16X8;
  825. for (i = 0; i < 2; i++) {
  826. if (USES_LIST(mb_type, i)) {
  827. /* MT_16X8 */
  828. for (j = 0; j < 2; j++) {
  829. s->field_select[i][j] = get_bits1(&s->gb);
  830. for (k = 0; k < 2; k++) {
  831. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  832. s->last_mv[i][j][k]);
  833. s->last_mv[i][j][k] = val;
  834. s->mv[i][j][k] = val;
  835. }
  836. }
  837. }
  838. }
  839. }
  840. break;
  841. case MT_FIELD:
  842. s->mv_type = MV_TYPE_FIELD;
  843. if (s->picture_structure == PICT_FRAME) {
  844. mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED;
  845. for (i = 0; i < 2; i++) {
  846. if (USES_LIST(mb_type, i)) {
  847. for (j = 0; j < 2; j++) {
  848. s->field_select[i][j] = get_bits1(&s->gb);
  849. val = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  850. s->last_mv[i][j][0]);
  851. s->last_mv[i][j][0] = val;
  852. s->mv[i][j][0] = val;
  853. av_dlog(s->avctx, "fmx=%d\n", val);
  854. val = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  855. s->last_mv[i][j][1] >> 1);
  856. s->last_mv[i][j][1] = val << 1;
  857. s->mv[i][j][1] = val;
  858. av_dlog(s->avctx, "fmy=%d\n", val);
  859. }
  860. }
  861. }
  862. } else {
  863. av_assert0(!s->progressive_sequence);
  864. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  865. for (i = 0; i < 2; i++) {
  866. if (USES_LIST(mb_type, i)) {
  867. s->field_select[i][0] = get_bits1(&s->gb);
  868. for (k = 0; k < 2; k++) {
  869. val = mpeg_decode_motion(s, s->mpeg_f_code[i][k],
  870. s->last_mv[i][0][k]);
  871. s->last_mv[i][0][k] = val;
  872. s->last_mv[i][1][k] = val;
  873. s->mv[i][0][k] = val;
  874. }
  875. }
  876. }
  877. }
  878. break;
  879. case MT_DMV:
  880. if(s->progressive_sequence){
  881. av_log(s->avctx, AV_LOG_ERROR, "MT_DMV in progressive_sequence\n");
  882. return -1;
  883. }
  884. s->mv_type = MV_TYPE_DMV;
  885. for (i = 0; i < 2; i++) {
  886. if (USES_LIST(mb_type, i)) {
  887. int dmx, dmy, mx, my, m;
  888. const int my_shift = s->picture_structure == PICT_FRAME;
  889. mx = mpeg_decode_motion(s, s->mpeg_f_code[i][0],
  890. s->last_mv[i][0][0]);
  891. s->last_mv[i][0][0] = mx;
  892. s->last_mv[i][1][0] = mx;
  893. dmx = get_dmv(s);
  894. my = mpeg_decode_motion(s, s->mpeg_f_code[i][1],
  895. s->last_mv[i][0][1] >> my_shift);
  896. dmy = get_dmv(s);
  897. s->last_mv[i][0][1] = my << my_shift;
  898. s->last_mv[i][1][1] = my << my_shift;
  899. s->mv[i][0][0] = mx;
  900. s->mv[i][0][1] = my;
  901. s->mv[i][1][0] = mx; // not used
  902. s->mv[i][1][1] = my; // not used
  903. if (s->picture_structure == PICT_FRAME) {
  904. mb_type |= MB_TYPE_16x16 | MB_TYPE_INTERLACED;
  905. // m = 1 + 2 * s->top_field_first;
  906. m = s->top_field_first ? 1 : 3;
  907. /* top -> top pred */
  908. s->mv[i][2][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  909. s->mv[i][2][1] = ((my * m + (my > 0)) >> 1) + dmy - 1;
  910. m = 4 - m;
  911. s->mv[i][3][0] = ((mx * m + (mx > 0)) >> 1) + dmx;
  912. s->mv[i][3][1] = ((my * m + (my > 0)) >> 1) + dmy + 1;
  913. } else {
  914. mb_type |= MB_TYPE_16x16;
  915. s->mv[i][2][0] = ((mx + (mx > 0)) >> 1) + dmx;
  916. s->mv[i][2][1] = ((my + (my > 0)) >> 1) + dmy;
  917. if (s->picture_structure == PICT_TOP_FIELD)
  918. s->mv[i][2][1]--;
  919. else
  920. s->mv[i][2][1]++;
  921. }
  922. }
  923. }
  924. break;
  925. default:
  926. av_log(s->avctx, AV_LOG_ERROR, "00 motion_type at %d %d\n", s->mb_x, s->mb_y);
  927. return -1;
  928. }
  929. }
  930. s->mb_intra = 0;
  931. if (HAS_CBP(mb_type)) {
  932. s->dsp.clear_blocks(s->block[0]);
  933. cbp = get_vlc2(&s->gb, mb_pat_vlc.table, MB_PAT_VLC_BITS, 1);
  934. if (mb_block_count > 6) {
  935. cbp <<= mb_block_count - 6;
  936. cbp |= get_bits(&s->gb, mb_block_count - 6);
  937. s->dsp.clear_blocks(s->block[6]);
  938. }
  939. if (cbp <= 0) {
  940. av_log(s->avctx, AV_LOG_ERROR, "invalid cbp at %d %d\n", s->mb_x, s->mb_y);
  941. return -1;
  942. }
  943. //if 1, we memcpy blocks in xvmcvideo
  944. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1) {
  945. ff_xvmc_pack_pblocks(s, cbp);
  946. if (s->swap_uv) {
  947. exchange_uv(s);
  948. }
  949. }
  950. if (s->codec_id == CODEC_ID_MPEG2VIDEO) {
  951. if (s->flags2 & CODEC_FLAG2_FAST) {
  952. for (i = 0; i < 6; i++) {
  953. if (cbp & 32) {
  954. mpeg2_fast_decode_block_non_intra(s, *s->pblocks[i], i);
  955. } else {
  956. s->block_last_index[i] = -1;
  957. }
  958. cbp += cbp;
  959. }
  960. } else {
  961. cbp <<= 12-mb_block_count;
  962. for (i = 0; i < mb_block_count; i++) {
  963. if (cbp & (1 << 11)) {
  964. if (mpeg2_decode_block_non_intra(s, *s->pblocks[i], i) < 0)
  965. return -1;
  966. } else {
  967. s->block_last_index[i] = -1;
  968. }
  969. cbp += cbp;
  970. }
  971. }
  972. } else {
  973. if (s->flags2 & CODEC_FLAG2_FAST) {
  974. for (i = 0; i < 6; i++) {
  975. if (cbp & 32) {
  976. mpeg1_fast_decode_block_inter(s, *s->pblocks[i], i);
  977. } else {
  978. s->block_last_index[i] = -1;
  979. }
  980. cbp += cbp;
  981. }
  982. } else {
  983. for (i = 0; i < 6; i++) {
  984. if (cbp & 32) {
  985. if (mpeg1_decode_block_inter(s, *s->pblocks[i], i) < 0)
  986. return -1;
  987. } else {
  988. s->block_last_index[i] = -1;
  989. }
  990. cbp += cbp;
  991. }
  992. }
  993. }
  994. } else {
  995. for (i = 0; i < 12; i++)
  996. s->block_last_index[i] = -1;
  997. }
  998. }
  999. s->current_picture.f.mb_type[s->mb_x + s->mb_y * s->mb_stride] = mb_type;
  1000. return 0;
  1001. }
  1002. static av_cold int mpeg_decode_init(AVCodecContext *avctx)
  1003. {
  1004. Mpeg1Context *s = avctx->priv_data;
  1005. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1006. int i;
  1007. /* we need some permutation to store matrices,
  1008. * until MPV_common_init() sets the real permutation. */
  1009. for (i = 0; i < 64; i++)
  1010. s2->dsp.idct_permutation[i]=i;
  1011. ff_MPV_decode_defaults(s2);
  1012. s->mpeg_enc_ctx.avctx = avctx;
  1013. s->mpeg_enc_ctx.flags = avctx->flags;
  1014. s->mpeg_enc_ctx.flags2 = avctx->flags2;
  1015. ff_mpeg12_common_init(&s->mpeg_enc_ctx);
  1016. ff_mpeg12_init_vlcs();
  1017. s->mpeg_enc_ctx_allocated = 0;
  1018. s->mpeg_enc_ctx.picture_number = 0;
  1019. s->repeat_field = 0;
  1020. s->mpeg_enc_ctx.codec_id = avctx->codec->id;
  1021. avctx->color_range = AVCOL_RANGE_MPEG;
  1022. if (avctx->codec->id == CODEC_ID_MPEG1VIDEO)
  1023. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  1024. else
  1025. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  1026. return 0;
  1027. }
  1028. static int mpeg_decode_update_thread_context(AVCodecContext *avctx, const AVCodecContext *avctx_from)
  1029. {
  1030. Mpeg1Context *ctx = avctx->priv_data, *ctx_from = avctx_from->priv_data;
  1031. MpegEncContext *s = &ctx->mpeg_enc_ctx, *s1 = &ctx_from->mpeg_enc_ctx;
  1032. int err;
  1033. if (avctx == avctx_from || !ctx_from->mpeg_enc_ctx_allocated || !s1->context_initialized)
  1034. return 0;
  1035. err = ff_mpeg_update_thread_context(avctx, avctx_from);
  1036. if (err) return err;
  1037. if (!ctx->mpeg_enc_ctx_allocated)
  1038. memcpy(s + 1, s1 + 1, sizeof(Mpeg1Context) - sizeof(MpegEncContext));
  1039. if (!(s->pict_type == AV_PICTURE_TYPE_B || s->low_delay))
  1040. s->picture_number++;
  1041. return 0;
  1042. }
  1043. static void quant_matrix_rebuild(uint16_t *matrix, const uint8_t *old_perm,
  1044. const uint8_t *new_perm)
  1045. {
  1046. uint16_t temp_matrix[64];
  1047. int i;
  1048. memcpy(temp_matrix, matrix, 64 * sizeof(uint16_t));
  1049. for (i = 0; i < 64; i++) {
  1050. matrix[new_perm[i]] = temp_matrix[old_perm[i]];
  1051. }
  1052. }
  1053. static const enum PixelFormat mpeg1_hwaccel_pixfmt_list_420[] = {
  1054. #if CONFIG_MPEG_XVMC_DECODER
  1055. PIX_FMT_XVMC_MPEG2_IDCT,
  1056. PIX_FMT_XVMC_MPEG2_MC,
  1057. #endif
  1058. #if CONFIG_MPEG1_VDPAU_HWACCEL
  1059. PIX_FMT_VDPAU_MPEG1,
  1060. #endif
  1061. PIX_FMT_YUV420P,
  1062. PIX_FMT_NONE
  1063. };
  1064. static const enum PixelFormat mpeg2_hwaccel_pixfmt_list_420[] = {
  1065. #if CONFIG_MPEG_XVMC_DECODER
  1066. PIX_FMT_XVMC_MPEG2_IDCT,
  1067. PIX_FMT_XVMC_MPEG2_MC,
  1068. #endif
  1069. #if CONFIG_MPEG2_VDPAU_HWACCEL
  1070. PIX_FMT_VDPAU_MPEG2,
  1071. #endif
  1072. #if CONFIG_MPEG2_DXVA2_HWACCEL
  1073. PIX_FMT_DXVA2_VLD,
  1074. #endif
  1075. #if CONFIG_MPEG2_VAAPI_HWACCEL
  1076. PIX_FMT_VAAPI_VLD,
  1077. #endif
  1078. PIX_FMT_YUV420P,
  1079. PIX_FMT_NONE
  1080. };
  1081. static inline int uses_vdpau(AVCodecContext *avctx) {
  1082. return avctx->pix_fmt == PIX_FMT_VDPAU_MPEG1 || avctx->pix_fmt == PIX_FMT_VDPAU_MPEG2;
  1083. }
  1084. static enum PixelFormat mpeg_get_pixelformat(AVCodecContext *avctx)
  1085. {
  1086. Mpeg1Context *s1 = avctx->priv_data;
  1087. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1088. if(s->chroma_format < 2) {
  1089. enum PixelFormat res;
  1090. res = avctx->get_format(avctx,
  1091. avctx->codec_id == CODEC_ID_MPEG1VIDEO ?
  1092. mpeg1_hwaccel_pixfmt_list_420 :
  1093. mpeg2_hwaccel_pixfmt_list_420);
  1094. if (res != PIX_FMT_XVMC_MPEG2_IDCT && res != PIX_FMT_XVMC_MPEG2_MC) {
  1095. avctx->xvmc_acceleration = 0;
  1096. } else if (!avctx->xvmc_acceleration) {
  1097. avctx->xvmc_acceleration = 2;
  1098. }
  1099. return res;
  1100. } else if(s->chroma_format == 2)
  1101. return PIX_FMT_YUV422P;
  1102. else
  1103. return PIX_FMT_YUV444P;
  1104. }
  1105. /* Call this function when we know all parameters.
  1106. * It may be called in different places for MPEG-1 and MPEG-2. */
  1107. static int mpeg_decode_postinit(AVCodecContext *avctx)
  1108. {
  1109. Mpeg1Context *s1 = avctx->priv_data;
  1110. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1111. uint8_t old_permutation[64];
  1112. if ((s1->mpeg_enc_ctx_allocated == 0) ||
  1113. avctx->coded_width != s->width ||
  1114. avctx->coded_height != s->height ||
  1115. s1->save_width != s->width ||
  1116. s1->save_height != s->height ||
  1117. s1->save_aspect_info != s->aspect_ratio_info ||
  1118. s1->save_progressive_seq != s->progressive_sequence ||
  1119. 0)
  1120. {
  1121. if (s1->mpeg_enc_ctx_allocated) {
  1122. ParseContext pc = s->parse_context;
  1123. s->parse_context.buffer = 0;
  1124. ff_MPV_common_end(s);
  1125. s->parse_context = pc;
  1126. }
  1127. if ((s->width == 0) || (s->height == 0))
  1128. return -2;
  1129. avcodec_set_dimensions(avctx, s->width, s->height);
  1130. avctx->bit_rate = s->bit_rate;
  1131. s1->save_aspect_info = s->aspect_ratio_info;
  1132. s1->save_width = s->width;
  1133. s1->save_height = s->height;
  1134. s1->save_progressive_seq = s->progressive_sequence;
  1135. /* low_delay may be forced, in this case we will have B-frames
  1136. * that behave like P-frames. */
  1137. avctx->has_b_frames = !s->low_delay;
  1138. if (avctx->codec_id == CODEC_ID_MPEG1VIDEO) {
  1139. //MPEG-1 fps
  1140. avctx->time_base.den = avpriv_frame_rate_tab[s->frame_rate_index].num;
  1141. avctx->time_base.num = avpriv_frame_rate_tab[s->frame_rate_index].den;
  1142. //MPEG-1 aspect
  1143. avctx->sample_aspect_ratio = av_d2q(1.0/ff_mpeg1_aspect[s->aspect_ratio_info], 255);
  1144. avctx->ticks_per_frame=1;
  1145. } else {//MPEG-2
  1146. //MPEG-2 fps
  1147. av_reduce(&s->avctx->time_base.den,
  1148. &s->avctx->time_base.num,
  1149. avpriv_frame_rate_tab[s->frame_rate_index].num * s1->frame_rate_ext.num*2,
  1150. avpriv_frame_rate_tab[s->frame_rate_index].den * s1->frame_rate_ext.den,
  1151. 1 << 30);
  1152. avctx->ticks_per_frame = 2;
  1153. //MPEG-2 aspect
  1154. if (s->aspect_ratio_info > 1) {
  1155. AVRational dar =
  1156. av_mul_q(av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1157. (AVRational) {s1->pan_scan.width, s1->pan_scan.height}),
  1158. (AVRational) {s->width, s->height});
  1159. // we ignore the spec here and guess a bit as reality does not match the spec, see for example
  1160. // res_change_ffmpeg_aspect.ts and sequence-display-aspect.mpg
  1161. // issue1613, 621, 562
  1162. if ((s1->pan_scan.width == 0) || (s1->pan_scan.height == 0) ||
  1163. (av_cmp_q(dar, (AVRational) {4, 3}) && av_cmp_q(dar, (AVRational) {16, 9}))) {
  1164. s->avctx->sample_aspect_ratio =
  1165. av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1166. (AVRational) {s->width, s->height});
  1167. } else {
  1168. s->avctx->sample_aspect_ratio =
  1169. av_div_q(ff_mpeg2_aspect[s->aspect_ratio_info],
  1170. (AVRational) {s1->pan_scan.width, s1->pan_scan.height});
  1171. //issue1613 4/3 16/9 -> 16/9
  1172. //res_change_ffmpeg_aspect.ts 4/3 225/44 ->4/3
  1173. //widescreen-issue562.mpg 4/3 16/9 -> 16/9
  1174. // s->avctx->sample_aspect_ratio = av_mul_q(s->avctx->sample_aspect_ratio, (AVRational) {s->width, s->height});
  1175. //av_log(NULL, AV_LOG_ERROR, "A %d/%d\n", ff_mpeg2_aspect[s->aspect_ratio_info].num, ff_mpeg2_aspect[s->aspect_ratio_info].den);
  1176. //av_log(NULL, AV_LOG_ERROR, "B %d/%d\n", s->avctx->sample_aspect_ratio.num, s->avctx->sample_aspect_ratio.den);
  1177. }
  1178. } else {
  1179. s->avctx->sample_aspect_ratio =
  1180. ff_mpeg2_aspect[s->aspect_ratio_info];
  1181. }
  1182. } // MPEG-2
  1183. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1184. avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
  1185. // until then pix_fmt may be changed right after codec init
  1186. if (avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT ||
  1187. avctx->hwaccel )
  1188. if (avctx->idct_algo == FF_IDCT_AUTO)
  1189. avctx->idct_algo = FF_IDCT_SIMPLE;
  1190. /* Quantization matrices may need reordering
  1191. * if DCT permutation is changed. */
  1192. memcpy(old_permutation, s->dsp.idct_permutation, 64 * sizeof(uint8_t));
  1193. if (ff_MPV_common_init(s) < 0)
  1194. return -2;
  1195. quant_matrix_rebuild(s->intra_matrix, old_permutation, s->dsp.idct_permutation);
  1196. quant_matrix_rebuild(s->inter_matrix, old_permutation, s->dsp.idct_permutation);
  1197. quant_matrix_rebuild(s->chroma_intra_matrix, old_permutation, s->dsp.idct_permutation);
  1198. quant_matrix_rebuild(s->chroma_inter_matrix, old_permutation, s->dsp.idct_permutation);
  1199. s1->mpeg_enc_ctx_allocated = 1;
  1200. }
  1201. return 0;
  1202. }
  1203. static int mpeg1_decode_picture(AVCodecContext *avctx,
  1204. const uint8_t *buf, int buf_size)
  1205. {
  1206. Mpeg1Context *s1 = avctx->priv_data;
  1207. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1208. int ref, f_code, vbv_delay;
  1209. init_get_bits(&s->gb, buf, buf_size*8);
  1210. ref = get_bits(&s->gb, 10); /* temporal ref */
  1211. s->pict_type = get_bits(&s->gb, 3);
  1212. if (s->pict_type == 0 || s->pict_type > 3)
  1213. return -1;
  1214. vbv_delay = get_bits(&s->gb, 16);
  1215. if (s->pict_type == AV_PICTURE_TYPE_P || s->pict_type == AV_PICTURE_TYPE_B) {
  1216. s->full_pel[0] = get_bits1(&s->gb);
  1217. f_code = get_bits(&s->gb, 3);
  1218. if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
  1219. return -1;
  1220. f_code += !f_code;
  1221. s->mpeg_f_code[0][0] = f_code;
  1222. s->mpeg_f_code[0][1] = f_code;
  1223. }
  1224. if (s->pict_type == AV_PICTURE_TYPE_B) {
  1225. s->full_pel[1] = get_bits1(&s->gb);
  1226. f_code = get_bits(&s->gb, 3);
  1227. if (f_code == 0 && (avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)))
  1228. return -1;
  1229. f_code += !f_code;
  1230. s->mpeg_f_code[1][0] = f_code;
  1231. s->mpeg_f_code[1][1] = f_code;
  1232. }
  1233. s->current_picture.f.pict_type = s->pict_type;
  1234. s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1235. if (avctx->debug & FF_DEBUG_PICT_INFO)
  1236. av_log(avctx, AV_LOG_DEBUG, "vbv_delay %d, ref %d type:%d\n", vbv_delay, ref, s->pict_type);
  1237. s->y_dc_scale = 8;
  1238. s->c_dc_scale = 8;
  1239. return 0;
  1240. }
  1241. static void mpeg_decode_sequence_extension(Mpeg1Context *s1)
  1242. {
  1243. MpegEncContext *s= &s1->mpeg_enc_ctx;
  1244. int horiz_size_ext, vert_size_ext;
  1245. int bit_rate_ext;
  1246. skip_bits(&s->gb, 1); /* profile and level esc*/
  1247. s->avctx->profile = get_bits(&s->gb, 3);
  1248. s->avctx->level = get_bits(&s->gb, 4);
  1249. s->progressive_sequence = get_bits1(&s->gb); /* progressive_sequence */
  1250. s->chroma_format = get_bits(&s->gb, 2); /* chroma_format 1=420, 2=422, 3=444 */
  1251. horiz_size_ext = get_bits(&s->gb, 2);
  1252. vert_size_ext = get_bits(&s->gb, 2);
  1253. s->width |= (horiz_size_ext << 12);
  1254. s->height |= (vert_size_ext << 12);
  1255. bit_rate_ext = get_bits(&s->gb, 12); /* XXX: handle it */
  1256. s->bit_rate += (bit_rate_ext << 18) * 400;
  1257. skip_bits1(&s->gb); /* marker */
  1258. s->avctx->rc_buffer_size += get_bits(&s->gb, 8) * 1024 * 16 << 10;
  1259. s->low_delay = get_bits1(&s->gb);
  1260. if (s->flags & CODEC_FLAG_LOW_DELAY)
  1261. s->low_delay = 1;
  1262. s1->frame_rate_ext.num = get_bits(&s->gb, 2) + 1;
  1263. s1->frame_rate_ext.den = get_bits(&s->gb, 5) + 1;
  1264. av_dlog(s->avctx, "sequence extension\n");
  1265. s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG2VIDEO;
  1266. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1267. av_log(s->avctx, AV_LOG_DEBUG, "profile: %d, level: %d vbv buffer: %d, bitrate:%d\n",
  1268. s->avctx->profile, s->avctx->level, s->avctx->rc_buffer_size, s->bit_rate);
  1269. }
  1270. static void mpeg_decode_sequence_display_extension(Mpeg1Context *s1)
  1271. {
  1272. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1273. int color_description, w, h;
  1274. skip_bits(&s->gb, 3); /* video format */
  1275. color_description = get_bits1(&s->gb);
  1276. if (color_description) {
  1277. s->avctx->color_primaries = get_bits(&s->gb, 8);
  1278. s->avctx->color_trc = get_bits(&s->gb, 8);
  1279. s->avctx->colorspace = get_bits(&s->gb, 8);
  1280. }
  1281. w = get_bits(&s->gb, 14);
  1282. skip_bits(&s->gb, 1); //marker
  1283. h = get_bits(&s->gb, 14);
  1284. // remaining 3 bits are zero padding
  1285. s1->pan_scan.width = 16 * w;
  1286. s1->pan_scan.height = 16 * h;
  1287. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1288. av_log(s->avctx, AV_LOG_DEBUG, "sde w:%d, h:%d\n", w, h);
  1289. }
  1290. static void mpeg_decode_picture_display_extension(Mpeg1Context *s1)
  1291. {
  1292. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1293. int i, nofco;
  1294. nofco = 1;
  1295. if (s->progressive_sequence) {
  1296. if (s->repeat_first_field) {
  1297. nofco++;
  1298. if (s->top_field_first)
  1299. nofco++;
  1300. }
  1301. } else {
  1302. if (s->picture_structure == PICT_FRAME) {
  1303. nofco++;
  1304. if (s->repeat_first_field)
  1305. nofco++;
  1306. }
  1307. }
  1308. for (i = 0; i < nofco; i++) {
  1309. s1->pan_scan.position[i][0] = get_sbits(&s->gb, 16);
  1310. skip_bits(&s->gb, 1); // marker
  1311. s1->pan_scan.position[i][1] = get_sbits(&s->gb, 16);
  1312. skip_bits(&s->gb, 1); // marker
  1313. }
  1314. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1315. av_log(s->avctx, AV_LOG_DEBUG, "pde (%d,%d) (%d,%d) (%d,%d)\n",
  1316. s1->pan_scan.position[0][0], s1->pan_scan.position[0][1],
  1317. s1->pan_scan.position[1][0], s1->pan_scan.position[1][1],
  1318. s1->pan_scan.position[2][0], s1->pan_scan.position[2][1]);
  1319. }
  1320. static int load_matrix(MpegEncContext *s, uint16_t matrix0[64], uint16_t matrix1[64], int intra)
  1321. {
  1322. int i;
  1323. for (i = 0; i < 64; i++) {
  1324. int j = s->dsp.idct_permutation[ff_zigzag_direct[i]];
  1325. int v = get_bits(&s->gb, 8);
  1326. if (v == 0) {
  1327. av_log(s->avctx, AV_LOG_ERROR, "matrix damaged\n");
  1328. return -1;
  1329. }
  1330. if (intra && i == 0 && v != 8) {
  1331. av_log(s->avctx, AV_LOG_DEBUG, "intra matrix specifies invalid DC quantizer %d, ignoring\n", v);
  1332. v = 8; // needed by pink.mpg / issue1046
  1333. }
  1334. matrix0[j] = v;
  1335. if (matrix1)
  1336. matrix1[j] = v;
  1337. }
  1338. return 0;
  1339. }
  1340. static void mpeg_decode_quant_matrix_extension(MpegEncContext *s)
  1341. {
  1342. av_dlog(s->avctx, "matrix extension\n");
  1343. if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1344. if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1345. if (get_bits1(&s->gb)) load_matrix(s, s->chroma_intra_matrix, NULL , 1);
  1346. if (get_bits1(&s->gb)) load_matrix(s, s->chroma_inter_matrix, NULL , 0);
  1347. }
  1348. static void mpeg_decode_picture_coding_extension(Mpeg1Context *s1)
  1349. {
  1350. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1351. s->full_pel[0] = s->full_pel[1] = 0;
  1352. s->mpeg_f_code[0][0] = get_bits(&s->gb, 4);
  1353. s->mpeg_f_code[0][1] = get_bits(&s->gb, 4);
  1354. s->mpeg_f_code[1][0] = get_bits(&s->gb, 4);
  1355. s->mpeg_f_code[1][1] = get_bits(&s->gb, 4);
  1356. if (!s->pict_type && s1->mpeg_enc_ctx_allocated) {
  1357. av_log(s->avctx, AV_LOG_ERROR, "Missing picture start code, guessing missing values\n");
  1358. if (s->mpeg_f_code[1][0] == 15 && s->mpeg_f_code[1][1] == 15) {
  1359. if (s->mpeg_f_code[0][0] == 15 && s->mpeg_f_code[0][1] == 15)
  1360. s->pict_type = AV_PICTURE_TYPE_I;
  1361. else
  1362. s->pict_type = AV_PICTURE_TYPE_P;
  1363. } else
  1364. s->pict_type = AV_PICTURE_TYPE_B;
  1365. s->current_picture.f.pict_type = s->pict_type;
  1366. s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  1367. }
  1368. s->mpeg_f_code[0][0] += !s->mpeg_f_code[0][0];
  1369. s->mpeg_f_code[0][1] += !s->mpeg_f_code[0][1];
  1370. s->mpeg_f_code[1][0] += !s->mpeg_f_code[1][0];
  1371. s->mpeg_f_code[1][1] += !s->mpeg_f_code[1][1];
  1372. s->intra_dc_precision = get_bits(&s->gb, 2);
  1373. s->picture_structure = get_bits(&s->gb, 2);
  1374. s->top_field_first = get_bits1(&s->gb);
  1375. s->frame_pred_frame_dct = get_bits1(&s->gb);
  1376. s->concealment_motion_vectors = get_bits1(&s->gb);
  1377. s->q_scale_type = get_bits1(&s->gb);
  1378. s->intra_vlc_format = get_bits1(&s->gb);
  1379. s->alternate_scan = get_bits1(&s->gb);
  1380. s->repeat_first_field = get_bits1(&s->gb);
  1381. s->chroma_420_type = get_bits1(&s->gb);
  1382. s->progressive_frame = get_bits1(&s->gb);
  1383. if (s->progressive_sequence && !s->progressive_frame) {
  1384. s->progressive_frame = 1;
  1385. av_log(s->avctx, AV_LOG_ERROR, "interlaced frame in progressive sequence, ignoring\n");
  1386. }
  1387. if (s->picture_structure == 0 || (s->progressive_frame && s->picture_structure != PICT_FRAME)) {
  1388. av_log(s->avctx, AV_LOG_ERROR, "picture_structure %d invalid, ignoring\n", s->picture_structure);
  1389. s->picture_structure = PICT_FRAME;
  1390. }
  1391. if (s->progressive_sequence && !s->frame_pred_frame_dct) {
  1392. av_log(s->avctx, AV_LOG_WARNING, "invalid frame_pred_frame_dct\n");
  1393. }
  1394. if (s->picture_structure == PICT_FRAME) {
  1395. s->first_field = 0;
  1396. s->v_edge_pos = 16 * s->mb_height;
  1397. } else {
  1398. s->first_field ^= 1;
  1399. s->v_edge_pos = 8 * s->mb_height;
  1400. memset(s->mbskip_table, 0, s->mb_stride * s->mb_height);
  1401. }
  1402. if (s->alternate_scan) {
  1403. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
  1404. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
  1405. } else {
  1406. ff_init_scantable(s->dsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
  1407. ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
  1408. }
  1409. /* composite display not parsed */
  1410. av_dlog(s->avctx, "intra_dc_precision=%d\n", s->intra_dc_precision);
  1411. av_dlog(s->avctx, "picture_structure=%d\n", s->picture_structure);
  1412. av_dlog(s->avctx, "top field first=%d\n", s->top_field_first);
  1413. av_dlog(s->avctx, "repeat first field=%d\n", s->repeat_first_field);
  1414. av_dlog(s->avctx, "conceal=%d\n", s->concealment_motion_vectors);
  1415. av_dlog(s->avctx, "intra_vlc_format=%d\n", s->intra_vlc_format);
  1416. av_dlog(s->avctx, "alternate_scan=%d\n", s->alternate_scan);
  1417. av_dlog(s->avctx, "frame_pred_frame_dct=%d\n", s->frame_pred_frame_dct);
  1418. av_dlog(s->avctx, "progressive_frame=%d\n", s->progressive_frame);
  1419. }
  1420. static int mpeg_field_start(MpegEncContext *s, const uint8_t *buf, int buf_size)
  1421. {
  1422. AVCodecContext *avctx = s->avctx;
  1423. Mpeg1Context *s1 = (Mpeg1Context*)s;
  1424. /* start frame decoding */
  1425. if (s->first_field || s->picture_structure == PICT_FRAME) {
  1426. if (ff_MPV_frame_start(s, avctx) < 0)
  1427. return -1;
  1428. ff_er_frame_start(s);
  1429. /* first check if we must repeat the frame */
  1430. s->current_picture_ptr->f.repeat_pict = 0;
  1431. if (s->repeat_first_field) {
  1432. if (s->progressive_sequence) {
  1433. if (s->top_field_first)
  1434. s->current_picture_ptr->f.repeat_pict = 4;
  1435. else
  1436. s->current_picture_ptr->f.repeat_pict = 2;
  1437. } else if (s->progressive_frame) {
  1438. s->current_picture_ptr->f.repeat_pict = 1;
  1439. }
  1440. }
  1441. *s->current_picture_ptr->f.pan_scan = s1->pan_scan;
  1442. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_FRAME))
  1443. ff_thread_finish_setup(avctx);
  1444. } else { // second field
  1445. int i;
  1446. if (!s->current_picture_ptr) {
  1447. av_log(s->avctx, AV_LOG_ERROR, "first field missing\n");
  1448. return -1;
  1449. }
  1450. if (s->avctx->hwaccel &&
  1451. (s->avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
  1452. if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
  1453. av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode first field\n");
  1454. }
  1455. for (i = 0; i < 4; i++) {
  1456. s->current_picture.f.data[i] = s->current_picture_ptr->f.data[i];
  1457. if (s->picture_structure == PICT_BOTTOM_FIELD) {
  1458. s->current_picture.f.data[i] += s->current_picture_ptr->f.linesize[i];
  1459. }
  1460. }
  1461. }
  1462. if (avctx->hwaccel) {
  1463. if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
  1464. return -1;
  1465. }
  1466. // MPV_frame_start will call this function too,
  1467. // but we need to call it on every field
  1468. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1469. if (ff_xvmc_field_start(s, avctx) < 0)
  1470. return -1;
  1471. return 0;
  1472. }
  1473. #define DECODE_SLICE_ERROR -1
  1474. #define DECODE_SLICE_OK 0
  1475. /**
  1476. * Decode a slice.
  1477. * MpegEncContext.mb_y must be set to the MB row from the startcode.
  1478. * @return DECODE_SLICE_ERROR if the slice is damaged,
  1479. * DECODE_SLICE_OK if this slice is OK
  1480. */
  1481. static int mpeg_decode_slice(MpegEncContext *s, int mb_y,
  1482. const uint8_t **buf, int buf_size)
  1483. {
  1484. AVCodecContext *avctx = s->avctx;
  1485. const int lowres = s->avctx->lowres;
  1486. const int field_pic = s->picture_structure != PICT_FRAME;
  1487. s->resync_mb_x =
  1488. s->resync_mb_y = -1;
  1489. av_assert0(mb_y < s->mb_height);
  1490. init_get_bits(&s->gb, *buf, buf_size * 8);
  1491. if(s->codec_id != CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
  1492. skip_bits(&s->gb, 3);
  1493. ff_mpeg1_clean_buffers(s);
  1494. s->interlaced_dct = 0;
  1495. s->qscale = get_qscale(s);
  1496. if (s->qscale == 0) {
  1497. av_log(s->avctx, AV_LOG_ERROR, "qscale == 0\n");
  1498. return -1;
  1499. }
  1500. /* extra slice info */
  1501. while (get_bits1(&s->gb) != 0) {
  1502. skip_bits(&s->gb, 8);
  1503. }
  1504. s->mb_x = 0;
  1505. if (mb_y == 0 && s->codec_tag == AV_RL32("SLIF")) {
  1506. skip_bits1(&s->gb);
  1507. } else {
  1508. while (get_bits_left(&s->gb) > 0) {
  1509. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1510. if (code < 0) {
  1511. av_log(s->avctx, AV_LOG_ERROR, "first mb_incr damaged\n");
  1512. return -1;
  1513. }
  1514. if (code >= 33) {
  1515. if (code == 33) {
  1516. s->mb_x += 33;
  1517. }
  1518. /* otherwise, stuffing, nothing to do */
  1519. } else {
  1520. s->mb_x += code;
  1521. break;
  1522. }
  1523. }
  1524. }
  1525. if (s->mb_x >= (unsigned)s->mb_width) {
  1526. av_log(s->avctx, AV_LOG_ERROR, "initial skip overflow\n");
  1527. return -1;
  1528. }
  1529. if (avctx->hwaccel) {
  1530. const uint8_t *buf_end, *buf_start = *buf - 4; /* include start_code */
  1531. int start_code = -1;
  1532. buf_end = avpriv_mpv_find_start_code(buf_start + 2, *buf + buf_size, &start_code);
  1533. if (buf_end < *buf + buf_size)
  1534. buf_end -= 4;
  1535. s->mb_y = mb_y;
  1536. if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_end - buf_start) < 0)
  1537. return DECODE_SLICE_ERROR;
  1538. *buf = buf_end;
  1539. return DECODE_SLICE_OK;
  1540. }
  1541. s->resync_mb_x = s->mb_x;
  1542. s->resync_mb_y = s->mb_y = mb_y;
  1543. s->mb_skip_run = 0;
  1544. ff_init_block_index(s);
  1545. if (s->mb_y == 0 && s->mb_x == 0 && (s->first_field || s->picture_structure == PICT_FRAME)) {
  1546. if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
  1547. av_log(s->avctx, AV_LOG_DEBUG, "qp:%d fc:%2d%2d%2d%2d %s %s %s %s %s dc:%d pstruct:%d fdct:%d cmv:%d qtype:%d ivlc:%d rff:%d %s\n",
  1548. s->qscale, s->mpeg_f_code[0][0], s->mpeg_f_code[0][1], s->mpeg_f_code[1][0], s->mpeg_f_code[1][1],
  1549. s->pict_type == AV_PICTURE_TYPE_I ? "I" : (s->pict_type == AV_PICTURE_TYPE_P ? "P" : (s->pict_type == AV_PICTURE_TYPE_B ? "B" : "S")),
  1550. s->progressive_sequence ? "ps" :"", s->progressive_frame ? "pf" : "", s->alternate_scan ? "alt" :"", s->top_field_first ? "top" :"",
  1551. s->intra_dc_precision, s->picture_structure, s->frame_pred_frame_dct, s->concealment_motion_vectors,
  1552. s->q_scale_type, s->intra_vlc_format, s->repeat_first_field, s->chroma_420_type ? "420" :"");
  1553. }
  1554. }
  1555. for (;;) {
  1556. // If 1, we memcpy blocks in xvmcvideo.
  1557. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration > 1)
  1558. ff_xvmc_init_block(s); // set s->block
  1559. if (mpeg_decode_mb(s, s->block) < 0)
  1560. return -1;
  1561. if (s->current_picture.f.motion_val[0] && !s->encoding) { // note motion_val is normally NULL unless we want to extract the MVs
  1562. const int wrap = s->b8_stride;
  1563. int xy = s->mb_x * 2 + s->mb_y * 2 * wrap;
  1564. int b8_xy = 4 * (s->mb_x + s->mb_y * s->mb_stride);
  1565. int motion_x, motion_y, dir, i;
  1566. for (i = 0; i < 2; i++) {
  1567. for (dir = 0; dir < 2; dir++) {
  1568. if (s->mb_intra || (dir == 1 && s->pict_type != AV_PICTURE_TYPE_B)) {
  1569. motion_x = motion_y = 0;
  1570. } else if (s->mv_type == MV_TYPE_16X16 || (s->mv_type == MV_TYPE_FIELD && field_pic)) {
  1571. motion_x = s->mv[dir][0][0];
  1572. motion_y = s->mv[dir][0][1];
  1573. } else /*if ((s->mv_type == MV_TYPE_FIELD) || (s->mv_type == MV_TYPE_16X8))*/ {
  1574. motion_x = s->mv[dir][i][0];
  1575. motion_y = s->mv[dir][i][1];
  1576. }
  1577. s->current_picture.f.motion_val[dir][xy ][0] = motion_x;
  1578. s->current_picture.f.motion_val[dir][xy ][1] = motion_y;
  1579. s->current_picture.f.motion_val[dir][xy + 1][0] = motion_x;
  1580. s->current_picture.f.motion_val[dir][xy + 1][1] = motion_y;
  1581. s->current_picture.f.ref_index [dir][b8_xy ] =
  1582. s->current_picture.f.ref_index [dir][b8_xy + 1] = s->field_select[dir][i];
  1583. av_assert2(s->field_select[dir][i] == 0 || s->field_select[dir][i] == 1);
  1584. }
  1585. xy += wrap;
  1586. b8_xy +=2;
  1587. }
  1588. }
  1589. s->dest[0] += 16 >> lowres;
  1590. s->dest[1] +=(16 >> lowres) >> s->chroma_x_shift;
  1591. s->dest[2] +=(16 >> lowres) >> s->chroma_x_shift;
  1592. ff_MPV_decode_mb(s, s->block);
  1593. if (++s->mb_x >= s->mb_width) {
  1594. const int mb_size = 16 >> s->avctx->lowres;
  1595. ff_draw_horiz_band(s, mb_size*(s->mb_y >> field_pic), mb_size);
  1596. ff_MPV_report_decode_progress(s);
  1597. s->mb_x = 0;
  1598. s->mb_y += 1 << field_pic;
  1599. if (s->mb_y >= s->mb_height) {
  1600. int left = get_bits_left(&s->gb);
  1601. int is_d10 = s->chroma_format == 2 && s->pict_type == AV_PICTURE_TYPE_I && avctx->profile == 0 && avctx->level == 5
  1602. && s->intra_dc_precision == 2 && s->q_scale_type == 1 && s->alternate_scan == 0
  1603. && s->progressive_frame == 0 /* vbv_delay == 0xBBB || 0xE10*/;
  1604. if (left < 0 || (left && show_bits(&s->gb, FFMIN(left, 23)) && !is_d10)
  1605. || ((avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) && left > 8)) {
  1606. av_log(avctx, AV_LOG_ERROR, "end mismatch left=%d %0X\n", left, show_bits(&s->gb, FFMIN(left, 23)));
  1607. return -1;
  1608. } else
  1609. goto eos;
  1610. }
  1611. ff_init_block_index(s);
  1612. }
  1613. /* skip mb handling */
  1614. if (s->mb_skip_run == -1) {
  1615. /* read increment again */
  1616. s->mb_skip_run = 0;
  1617. for (;;) {
  1618. int code = get_vlc2(&s->gb, mbincr_vlc.table, MBINCR_VLC_BITS, 2);
  1619. if (code < 0) {
  1620. av_log(s->avctx, AV_LOG_ERROR, "mb incr damaged\n");
  1621. return -1;
  1622. }
  1623. if (code >= 33) {
  1624. if (code == 33) {
  1625. s->mb_skip_run += 33;
  1626. } else if (code == 35) {
  1627. if (s->mb_skip_run != 0 || show_bits(&s->gb, 15) != 0) {
  1628. av_log(s->avctx, AV_LOG_ERROR, "slice mismatch\n");
  1629. return -1;
  1630. }
  1631. goto eos; /* end of slice */
  1632. }
  1633. /* otherwise, stuffing, nothing to do */
  1634. } else {
  1635. s->mb_skip_run += code;
  1636. break;
  1637. }
  1638. }
  1639. if (s->mb_skip_run) {
  1640. int i;
  1641. if (s->pict_type == AV_PICTURE_TYPE_I) {
  1642. av_log(s->avctx, AV_LOG_ERROR, "skipped MB in I frame at %d %d\n", s->mb_x, s->mb_y);
  1643. return -1;
  1644. }
  1645. /* skip mb */
  1646. s->mb_intra = 0;
  1647. for (i = 0; i < 12; i++)
  1648. s->block_last_index[i] = -1;
  1649. if (s->picture_structure == PICT_FRAME)
  1650. s->mv_type = MV_TYPE_16X16;
  1651. else
  1652. s->mv_type = MV_TYPE_FIELD;
  1653. if (s->pict_type == AV_PICTURE_TYPE_P) {
  1654. /* if P type, zero motion vector is implied */
  1655. s->mv_dir = MV_DIR_FORWARD;
  1656. s->mv[0][0][0] = s->mv[0][0][1] = 0;
  1657. s->last_mv[0][0][0] = s->last_mv[0][0][1] = 0;
  1658. s->last_mv[0][1][0] = s->last_mv[0][1][1] = 0;
  1659. s->field_select[0][0] = (s->picture_structure - 1) & 1;
  1660. } else {
  1661. /* if B type, reuse previous vectors and directions */
  1662. s->mv[0][0][0] = s->last_mv[0][0][0];
  1663. s->mv[0][0][1] = s->last_mv[0][0][1];
  1664. s->mv[1][0][0] = s->last_mv[1][0][0];
  1665. s->mv[1][0][1] = s->last_mv[1][0][1];
  1666. }
  1667. }
  1668. }
  1669. }
  1670. eos: // end of slice
  1671. *buf += (get_bits_count(&s->gb)-1)/8;
  1672. //printf("y %d %d %d %d\n", s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y);
  1673. return 0;
  1674. }
  1675. static int slice_decode_thread(AVCodecContext *c, void *arg)
  1676. {
  1677. MpegEncContext *s = *(void**)arg;
  1678. const uint8_t *buf = s->gb.buffer;
  1679. int mb_y = s->start_mb_y;
  1680. const int field_pic = s->picture_structure != PICT_FRAME;
  1681. s->error_count = (3 * (s->end_mb_y - s->start_mb_y) * s->mb_width) >> field_pic;
  1682. for (;;) {
  1683. uint32_t start_code;
  1684. int ret;
  1685. ret = mpeg_decode_slice(s, mb_y, &buf, s->gb.buffer_end - buf);
  1686. emms_c();
  1687. //av_log(c, AV_LOG_DEBUG, "ret:%d resync:%d/%d mb:%d/%d ts:%d/%d ec:%d\n",
  1688. //ret, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, s->start_mb_y, s->end_mb_y, s->error_count);
  1689. if (ret < 0) {
  1690. if (c->err_recognition & AV_EF_EXPLODE)
  1691. return ret;
  1692. if (s->resync_mb_x >= 0 && s->resync_mb_y >= 0)
  1693. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
  1694. } else {
  1695. ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_AC_END | ER_DC_END | ER_MV_END);
  1696. }
  1697. if (s->mb_y == s->end_mb_y)
  1698. return 0;
  1699. start_code = -1;
  1700. buf = avpriv_mpv_find_start_code(buf, s->gb.buffer_end, &start_code);
  1701. mb_y= start_code - SLICE_MIN_START_CODE;
  1702. if(s->codec_id != CODEC_ID_MPEG1VIDEO && s->mb_height > 2800/16)
  1703. mb_y += (*buf&0xE0)<<2;
  1704. mb_y <<= field_pic;
  1705. if (s->picture_structure == PICT_BOTTOM_FIELD)
  1706. mb_y++;
  1707. if (mb_y < 0 || mb_y >= s->end_mb_y)
  1708. return -1;
  1709. }
  1710. }
  1711. /**
  1712. * Handle slice ends.
  1713. * @return 1 if it seems to be the last slice
  1714. */
  1715. static int slice_end(AVCodecContext *avctx, AVFrame *pict)
  1716. {
  1717. Mpeg1Context *s1 = avctx->priv_data;
  1718. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1719. if (!s1->mpeg_enc_ctx_allocated || !s->current_picture_ptr)
  1720. return 0;
  1721. if (s->avctx->hwaccel) {
  1722. if (s->avctx->hwaccel->end_frame(s->avctx) < 0)
  1723. av_log(avctx, AV_LOG_ERROR, "hardware accelerator failed to decode picture\n");
  1724. }
  1725. if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
  1726. ff_xvmc_field_end(s);
  1727. /* end of slice reached */
  1728. if (/*s->mb_y << field_pic == s->mb_height &&*/ !s->first_field && !s->first_slice) {
  1729. /* end of image */
  1730. s->current_picture_ptr->f.qscale_type = FF_QSCALE_TYPE_MPEG2;
  1731. ff_er_frame_end(s);
  1732. ff_MPV_frame_end(s);
  1733. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  1734. *pict = s->current_picture_ptr->f;
  1735. ff_print_debug_info(s, pict);
  1736. } else {
  1737. if (avctx->active_thread_type & FF_THREAD_FRAME)
  1738. s->picture_number++;
  1739. /* latency of 1 frame for I- and P-frames */
  1740. /* XXX: use another variable than picture_number */
  1741. if (s->last_picture_ptr != NULL) {
  1742. *pict = s->last_picture_ptr->f;
  1743. ff_print_debug_info(s, pict);
  1744. }
  1745. }
  1746. return 1;
  1747. } else {
  1748. return 0;
  1749. }
  1750. }
  1751. static int mpeg1_decode_sequence(AVCodecContext *avctx,
  1752. const uint8_t *buf, int buf_size)
  1753. {
  1754. Mpeg1Context *s1 = avctx->priv_data;
  1755. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1756. int width, height;
  1757. int i, v, j;
  1758. init_get_bits(&s->gb, buf, buf_size*8);
  1759. width = get_bits(&s->gb, 12);
  1760. height = get_bits(&s->gb, 12);
  1761. if (width <= 0 || height <= 0)
  1762. return -1;
  1763. s->aspect_ratio_info = get_bits(&s->gb, 4);
  1764. if (s->aspect_ratio_info == 0) {
  1765. av_log(avctx, AV_LOG_ERROR, "aspect ratio has forbidden 0 value\n");
  1766. if (avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
  1767. return -1;
  1768. }
  1769. s->frame_rate_index = get_bits(&s->gb, 4);
  1770. if (s->frame_rate_index == 0 || s->frame_rate_index > 13)
  1771. return -1;
  1772. s->bit_rate = get_bits(&s->gb, 18) * 400;
  1773. if (get_bits1(&s->gb) == 0) /* marker */
  1774. return -1;
  1775. s->width = width;
  1776. s->height = height;
  1777. s->avctx->rc_buffer_size = get_bits(&s->gb, 10) * 1024 * 16;
  1778. skip_bits(&s->gb, 1);
  1779. /* get matrix */
  1780. if (get_bits1(&s->gb)) {
  1781. load_matrix(s, s->chroma_intra_matrix, s->intra_matrix, 1);
  1782. } else {
  1783. for (i = 0; i < 64; i++) {
  1784. j = s->dsp.idct_permutation[i];
  1785. v = ff_mpeg1_default_intra_matrix[i];
  1786. s->intra_matrix[j] = v;
  1787. s->chroma_intra_matrix[j] = v;
  1788. }
  1789. }
  1790. if (get_bits1(&s->gb)) {
  1791. load_matrix(s, s->chroma_inter_matrix, s->inter_matrix, 0);
  1792. } else {
  1793. for (i = 0; i < 64; i++) {
  1794. int j = s->dsp.idct_permutation[i];
  1795. v = ff_mpeg1_default_non_intra_matrix[i];
  1796. s->inter_matrix[j] = v;
  1797. s->chroma_inter_matrix[j] = v;
  1798. }
  1799. }
  1800. if (show_bits(&s->gb, 23) != 0) {
  1801. av_log(s->avctx, AV_LOG_ERROR, "sequence header damaged\n");
  1802. return -1;
  1803. }
  1804. /* we set MPEG-2 parameters so that it emulates MPEG-1 */
  1805. s->progressive_sequence = 1;
  1806. s->progressive_frame = 1;
  1807. s->picture_structure = PICT_FRAME;
  1808. s->first_field = 0;
  1809. s->frame_pred_frame_dct = 1;
  1810. s->chroma_format = 1;
  1811. s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG1VIDEO;
  1812. s->out_format = FMT_MPEG1;
  1813. s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
  1814. if (s->flags & CODEC_FLAG_LOW_DELAY)
  1815. s->low_delay = 1;
  1816. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1817. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
  1818. s->avctx->rc_buffer_size, s->bit_rate);
  1819. return 0;
  1820. }
  1821. static int vcr2_init_sequence(AVCodecContext *avctx)
  1822. {
  1823. Mpeg1Context *s1 = avctx->priv_data;
  1824. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1825. int i, v;
  1826. /* start new MPEG-1 context decoding */
  1827. s->out_format = FMT_MPEG1;
  1828. if (s1->mpeg_enc_ctx_allocated) {
  1829. ff_MPV_common_end(s);
  1830. }
  1831. s->width = avctx->coded_width;
  1832. s->height = avctx->coded_height;
  1833. avctx->has_b_frames = 0; // true?
  1834. s->low_delay = 1;
  1835. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1836. avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
  1837. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel )
  1838. if (avctx->idct_algo == FF_IDCT_AUTO)
  1839. avctx->idct_algo = FF_IDCT_SIMPLE;
  1840. if (ff_MPV_common_init(s) < 0)
  1841. return -1;
  1842. s1->mpeg_enc_ctx_allocated = 1;
  1843. for (i = 0; i < 64; i++) {
  1844. int j = s->dsp.idct_permutation[i];
  1845. v = ff_mpeg1_default_intra_matrix[i];
  1846. s->intra_matrix[j] = v;
  1847. s->chroma_intra_matrix[j] = v;
  1848. v = ff_mpeg1_default_non_intra_matrix[i];
  1849. s->inter_matrix[j] = v;
  1850. s->chroma_inter_matrix[j] = v;
  1851. }
  1852. s->progressive_sequence = 1;
  1853. s->progressive_frame = 1;
  1854. s->picture_structure = PICT_FRAME;
  1855. s->first_field = 0;
  1856. s->frame_pred_frame_dct = 1;
  1857. s->chroma_format = 1;
  1858. if (s->codec_tag == AV_RL32("BW10")) {
  1859. s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG1VIDEO;
  1860. } else {
  1861. exchange_uv(s); // common init reset pblocks, so we swap them here
  1862. s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
  1863. s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG2VIDEO;
  1864. }
  1865. s1->save_width = s->width;
  1866. s1->save_height = s->height;
  1867. s1->save_progressive_seq = s->progressive_sequence;
  1868. return 0;
  1869. }
  1870. static void mpeg_decode_user_data(AVCodecContext *avctx,
  1871. const uint8_t *p, int buf_size)
  1872. {
  1873. Mpeg1Context *s = avctx->priv_data;
  1874. const uint8_t *buf_end = p + buf_size;
  1875. if(buf_size > 29){
  1876. int i;
  1877. for(i=0; i<20; i++)
  1878. if(!memcmp(p+i, "\0TMPGEXS\0", 9)){
  1879. s->tmpgexs= 1;
  1880. }
  1881. /* for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
  1882. av_log(0,0, "%c", p[i]);
  1883. }
  1884. av_log(0,0, "\n");*/
  1885. }
  1886. /* we parse the DTG active format information */
  1887. if (buf_end - p >= 5 &&
  1888. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  1889. int flags = p[4];
  1890. p += 5;
  1891. if (flags & 0x80) {
  1892. /* skip event id */
  1893. p += 2;
  1894. }
  1895. if (flags & 0x40) {
  1896. if (buf_end - p < 1)
  1897. return;
  1898. avctx->dtg_active_format = p[0] & 0x0f;
  1899. }
  1900. }
  1901. }
  1902. static void mpeg_decode_gop(AVCodecContext *avctx,
  1903. const uint8_t *buf, int buf_size)
  1904. {
  1905. Mpeg1Context *s1 = avctx->priv_data;
  1906. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1907. int broken_link;
  1908. int64_t tc;
  1909. init_get_bits(&s->gb, buf, buf_size*8);
  1910. tc = avctx->timecode_frame_start = get_bits(&s->gb, 25);
  1911. s->closed_gop = get_bits1(&s->gb);
  1912. /*broken_link indicate that after editing the
  1913. reference frames of the first B-Frames after GOP I-Frame
  1914. are missing (open gop)*/
  1915. broken_link = get_bits1(&s->gb);
  1916. if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
  1917. char tcbuf[AV_TIMECODE_STR_SIZE];
  1918. av_timecode_make_mpeg_tc_string(tcbuf, tc);
  1919. av_log(s->avctx, AV_LOG_DEBUG,
  1920. "GOP (%s) closed_gop=%d broken_link=%d\n",
  1921. tcbuf, s->closed_gop, broken_link);
  1922. }
  1923. }
  1924. /**
  1925. * Find the end of the current frame in the bitstream.
  1926. * @return the position of the first byte of the next frame, or -1
  1927. */
  1928. int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
  1929. {
  1930. int i;
  1931. uint32_t state = pc->state;
  1932. /* EOF considered as end of frame */
  1933. if (buf_size == 0)
  1934. return 0;
  1935. /*
  1936. 0 frame start -> 1/4
  1937. 1 first_SEQEXT -> 0/2
  1938. 2 first field start -> 3/0
  1939. 3 second_SEQEXT -> 2/0
  1940. 4 searching end
  1941. */
  1942. for (i = 0; i < buf_size; i++) {
  1943. av_assert1(pc->frame_start_found >= 0 && pc->frame_start_found <= 4);
  1944. if (pc->frame_start_found & 1) {
  1945. if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80)
  1946. pc->frame_start_found--;
  1947. else if (state == EXT_START_CODE + 2) {
  1948. if ((buf[i] & 3) == 3)
  1949. pc->frame_start_found = 0;
  1950. else
  1951. pc->frame_start_found = (pc->frame_start_found + 1) & 3;
  1952. }
  1953. state++;
  1954. } else {
  1955. i = avpriv_mpv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
  1956. if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
  1957. i++;
  1958. pc->frame_start_found = 4;
  1959. }
  1960. if (state == SEQ_END_CODE) {
  1961. pc->frame_start_found = 0;
  1962. pc->state=-1;
  1963. return i+1;
  1964. }
  1965. if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
  1966. pc->frame_start_found = 0;
  1967. if (pc->frame_start_found < 4 && state == EXT_START_CODE)
  1968. pc->frame_start_found++;
  1969. if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
  1970. if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) {
  1971. pc->frame_start_found = 0;
  1972. pc->state = -1;
  1973. return i - 3;
  1974. }
  1975. }
  1976. if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
  1977. ff_fetch_timestamp(s, i - 3, 1);
  1978. }
  1979. }
  1980. }
  1981. pc->state = state;
  1982. return END_NOT_FOUND;
  1983. }
  1984. static int decode_chunks(AVCodecContext *avctx,
  1985. AVFrame *picture, int *data_size,
  1986. const uint8_t *buf, int buf_size);
  1987. /* handle buffering and image synchronisation */
  1988. static int mpeg_decode_frame(AVCodecContext *avctx,
  1989. void *data, int *data_size,
  1990. AVPacket *avpkt)
  1991. {
  1992. const uint8_t *buf = avpkt->data;
  1993. int buf_size = avpkt->size;
  1994. Mpeg1Context *s = avctx->priv_data;
  1995. AVFrame *picture = data;
  1996. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1997. av_dlog(avctx, "fill_buffer\n");
  1998. if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
  1999. /* special case for last picture */
  2000. if (s2->low_delay == 0 && s2->next_picture_ptr) {
  2001. *picture = s2->next_picture_ptr->f;
  2002. s2->next_picture_ptr = NULL;
  2003. *data_size = sizeof(AVFrame);
  2004. }
  2005. return buf_size;
  2006. }
  2007. if (s2->flags & CODEC_FLAG_TRUNCATED) {
  2008. int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
  2009. if (ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0)
  2010. return buf_size;
  2011. }
  2012. s2->codec_tag = avpriv_toupper4(avctx->codec_tag);
  2013. if (s->mpeg_enc_ctx_allocated == 0 && ( s2->codec_tag == AV_RL32("VCR2")
  2014. || s2->codec_tag == AV_RL32("BW10")
  2015. ))
  2016. vcr2_init_sequence(avctx);
  2017. s->slice_count = 0;
  2018. if (avctx->extradata && !s->parsed_extra) {
  2019. int ret = decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
  2020. if(*data_size) {
  2021. av_log(avctx, AV_LOG_ERROR, "picture in extradata\n");
  2022. *data_size = 0;
  2023. }
  2024. s->parsed_extra = 1;
  2025. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
  2026. return ret;
  2027. }
  2028. return decode_chunks(avctx, picture, data_size, buf, buf_size);
  2029. }
  2030. static int decode_chunks(AVCodecContext *avctx,
  2031. AVFrame *picture, int *data_size,
  2032. const uint8_t *buf, int buf_size)
  2033. {
  2034. Mpeg1Context *s = avctx->priv_data;
  2035. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2036. const uint8_t *buf_ptr = buf;
  2037. const uint8_t *buf_end = buf + buf_size;
  2038. int ret, input_size;
  2039. int last_code = 0;
  2040. for (;;) {
  2041. /* find next start code */
  2042. uint32_t start_code = -1;
  2043. buf_ptr = avpriv_mpv_find_start_code(buf_ptr, buf_end, &start_code);
  2044. if (start_code > 0x1ff) {
  2045. if (s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT) {
  2046. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
  2047. int i;
  2048. av_assert0(avctx->thread_count > 1);
  2049. avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
  2050. for (i = 0; i < s->slice_count; i++)
  2051. s2->error_count += s2->thread_context[i]->error_count;
  2052. }
  2053. if (CONFIG_VDPAU && uses_vdpau(avctx))
  2054. ff_vdpau_mpeg_picture_complete(s2, buf, buf_size, s->slice_count);
  2055. if (slice_end(avctx, picture)) {
  2056. if (s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
  2057. *data_size = sizeof(AVPicture);
  2058. }
  2059. }
  2060. s2->pict_type = 0;
  2061. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  2062. }
  2063. input_size = buf_end - buf_ptr;
  2064. if (avctx->debug & FF_DEBUG_STARTCODE) {
  2065. av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
  2066. }
  2067. /* prepare data for next start code */
  2068. switch (start_code) {
  2069. case SEQ_START_CODE:
  2070. if (last_code == 0) {
  2071. mpeg1_decode_sequence(avctx, buf_ptr, input_size);
  2072. if(buf != avctx->extradata)
  2073. s->sync=1;
  2074. } else {
  2075. av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
  2076. if (avctx->err_recognition & AV_EF_EXPLODE)
  2077. return AVERROR_INVALIDDATA;
  2078. }
  2079. break;
  2080. case PICTURE_START_CODE:
  2081. if(s->tmpgexs){
  2082. s2->intra_dc_precision= 3;
  2083. s2->intra_matrix[0]= 1;
  2084. }
  2085. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && s->slice_count) {
  2086. int i;
  2087. avctx->execute(avctx, slice_decode_thread,
  2088. s2->thread_context, NULL,
  2089. s->slice_count, sizeof(void*));
  2090. for (i = 0; i < s->slice_count; i++)
  2091. s2->error_count += s2->thread_context[i]->error_count;
  2092. s->slice_count = 0;
  2093. }
  2094. if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
  2095. ret = mpeg_decode_postinit(avctx);
  2096. if (ret < 0) {
  2097. av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
  2098. return ret;
  2099. }
  2100. /* we have a complete image: we try to decompress it */
  2101. if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
  2102. s2->pict_type = 0;
  2103. s2->first_slice = 1;
  2104. last_code = PICTURE_START_CODE;
  2105. } else {
  2106. av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
  2107. if (avctx->err_recognition & AV_EF_EXPLODE)
  2108. return AVERROR_INVALIDDATA;
  2109. }
  2110. break;
  2111. case EXT_START_CODE:
  2112. init_get_bits(&s2->gb, buf_ptr, input_size*8);
  2113. switch (get_bits(&s2->gb, 4)) {
  2114. case 0x1:
  2115. if (last_code == 0) {
  2116. mpeg_decode_sequence_extension(s);
  2117. } else {
  2118. av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
  2119. if (avctx->err_recognition & AV_EF_EXPLODE)
  2120. return AVERROR_INVALIDDATA;
  2121. }
  2122. break;
  2123. case 0x2:
  2124. mpeg_decode_sequence_display_extension(s);
  2125. break;
  2126. case 0x3:
  2127. mpeg_decode_quant_matrix_extension(s2);
  2128. break;
  2129. case 0x7:
  2130. mpeg_decode_picture_display_extension(s);
  2131. break;
  2132. case 0x8:
  2133. if (last_code == PICTURE_START_CODE) {
  2134. mpeg_decode_picture_coding_extension(s);
  2135. } else {
  2136. av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
  2137. if (avctx->err_recognition & AV_EF_EXPLODE)
  2138. return AVERROR_INVALIDDATA;
  2139. }
  2140. break;
  2141. }
  2142. break;
  2143. case USER_START_CODE:
  2144. mpeg_decode_user_data(avctx, buf_ptr, input_size);
  2145. break;
  2146. case GOP_START_CODE:
  2147. if (last_code == 0) {
  2148. s2->first_field=0;
  2149. mpeg_decode_gop(avctx, buf_ptr, input_size);
  2150. s->sync=1;
  2151. } else {
  2152. av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
  2153. if (avctx->err_recognition & AV_EF_EXPLODE)
  2154. return AVERROR_INVALIDDATA;
  2155. }
  2156. break;
  2157. default:
  2158. if (start_code >= SLICE_MIN_START_CODE &&
  2159. start_code <= SLICE_MAX_START_CODE && last_code != 0) {
  2160. const int field_pic = s2->picture_structure != PICT_FRAME;
  2161. int mb_y = start_code - SLICE_MIN_START_CODE;
  2162. last_code = SLICE_MIN_START_CODE;
  2163. if(s2->codec_id != CODEC_ID_MPEG1VIDEO && s2->mb_height > 2800/16)
  2164. mb_y += (*buf_ptr&0xE0)<<2;
  2165. mb_y <<= field_pic;
  2166. if (s2->picture_structure == PICT_BOTTOM_FIELD)
  2167. mb_y++;
  2168. if (mb_y >= s2->mb_height) {
  2169. av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
  2170. return -1;
  2171. }
  2172. if (s2->last_picture_ptr == NULL) {
  2173. /* Skip B-frames if we do not have reference frames and gop is not closed */
  2174. if (s2->pict_type == AV_PICTURE_TYPE_B) {
  2175. if (!s2->closed_gop)
  2176. break;
  2177. }
  2178. }
  2179. if (s2->pict_type == AV_PICTURE_TYPE_I || (s2->flags2 & CODEC_FLAG2_SHOW_ALL))
  2180. s->sync=1;
  2181. if (s2->next_picture_ptr == NULL) {
  2182. /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
  2183. if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) break;
  2184. }
  2185. if ((avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type == AV_PICTURE_TYPE_B) ||
  2186. (avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type != AV_PICTURE_TYPE_I) ||
  2187. avctx->skip_frame >= AVDISCARD_ALL)
  2188. break;
  2189. if (!s->mpeg_enc_ctx_allocated)
  2190. break;
  2191. if (s2->codec_id == CODEC_ID_MPEG2VIDEO) {
  2192. if (mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
  2193. break;
  2194. }
  2195. if (!s2->pict_type) {
  2196. av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
  2197. if (avctx->err_recognition & AV_EF_EXPLODE)
  2198. return AVERROR_INVALIDDATA;
  2199. break;
  2200. }
  2201. if (s2->first_slice) {
  2202. s2->first_slice = 0;
  2203. if (mpeg_field_start(s2, buf, buf_size) < 0)
  2204. return -1;
  2205. }
  2206. if (!s2->current_picture_ptr) {
  2207. av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
  2208. return AVERROR_INVALIDDATA;
  2209. }
  2210. if (uses_vdpau(avctx)) {
  2211. s->slice_count++;
  2212. break;
  2213. }
  2214. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
  2215. int threshold = (s2->mb_height * s->slice_count +
  2216. s2->slice_context_count / 2) /
  2217. s2->slice_context_count;
  2218. av_assert0(avctx->thread_count > 1);
  2219. if (threshold <= mb_y) {
  2220. MpegEncContext *thread_context = s2->thread_context[s->slice_count];
  2221. thread_context->start_mb_y = mb_y;
  2222. thread_context->end_mb_y = s2->mb_height;
  2223. if (s->slice_count) {
  2224. s2->thread_context[s->slice_count-1]->end_mb_y = mb_y;
  2225. ff_update_duplicate_context(thread_context, s2);
  2226. }
  2227. init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
  2228. s->slice_count++;
  2229. }
  2230. buf_ptr += 2; // FIXME add minimum number of bytes per slice
  2231. } else {
  2232. ret = mpeg_decode_slice(s2, mb_y, &buf_ptr, input_size);
  2233. emms_c();
  2234. if (ret < 0) {
  2235. if (avctx->err_recognition & AV_EF_EXPLODE)
  2236. return ret;
  2237. if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
  2238. ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x, s2->mb_y, ER_AC_ERROR | ER_DC_ERROR | ER_MV_ERROR);
  2239. } else {
  2240. ff_er_add_slice(s2, s2->resync_mb_x, s2->resync_mb_y, s2->mb_x-1, s2->mb_y, ER_AC_END | ER_DC_END | ER_MV_END);
  2241. }
  2242. }
  2243. }
  2244. break;
  2245. }
  2246. }
  2247. }
  2248. static void flush(AVCodecContext *avctx)
  2249. {
  2250. Mpeg1Context *s = avctx->priv_data;
  2251. s->sync=0;
  2252. ff_mpeg_flush(avctx);
  2253. }
  2254. static int mpeg_decode_end(AVCodecContext *avctx)
  2255. {
  2256. Mpeg1Context *s = avctx->priv_data;
  2257. if (s->mpeg_enc_ctx_allocated)
  2258. ff_MPV_common_end(&s->mpeg_enc_ctx);
  2259. return 0;
  2260. }
  2261. static const AVProfile mpeg2_video_profiles[] = {
  2262. { FF_PROFILE_MPEG2_422, "4:2:2" },
  2263. { FF_PROFILE_MPEG2_HIGH, "High" },
  2264. { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
  2265. { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
  2266. { FF_PROFILE_MPEG2_MAIN, "Main" },
  2267. { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
  2268. { FF_PROFILE_RESERVED, "Reserved" },
  2269. { FF_PROFILE_RESERVED, "Reserved" },
  2270. { FF_PROFILE_UNKNOWN },
  2271. };
  2272. AVCodec ff_mpeg1video_decoder = {
  2273. .name = "mpeg1video",
  2274. .type = AVMEDIA_TYPE_VIDEO,
  2275. .id = CODEC_ID_MPEG1VIDEO,
  2276. .priv_data_size = sizeof(Mpeg1Context),
  2277. .init = mpeg_decode_init,
  2278. .close = mpeg_decode_end,
  2279. .decode = mpeg_decode_frame,
  2280. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  2281. CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
  2282. CODEC_CAP_SLICE_THREADS,
  2283. .flush = flush,
  2284. .max_lowres = 3,
  2285. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2286. .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
  2287. };
  2288. AVCodec ff_mpeg2video_decoder = {
  2289. .name = "mpeg2video",
  2290. .type = AVMEDIA_TYPE_VIDEO,
  2291. .id = CODEC_ID_MPEG2VIDEO,
  2292. .priv_data_size = sizeof(Mpeg1Context),
  2293. .init = mpeg_decode_init,
  2294. .close = mpeg_decode_end,
  2295. .decode = mpeg_decode_frame,
  2296. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  2297. CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY |
  2298. CODEC_CAP_SLICE_THREADS,
  2299. .flush = flush,
  2300. .max_lowres = 3,
  2301. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
  2302. .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
  2303. };
  2304. //legacy decoder
  2305. AVCodec ff_mpegvideo_decoder = {
  2306. .name = "mpegvideo",
  2307. .type = AVMEDIA_TYPE_VIDEO,
  2308. .id = CODEC_ID_MPEG2VIDEO,
  2309. .priv_data_size = sizeof(Mpeg1Context),
  2310. .init = mpeg_decode_init,
  2311. .close = mpeg_decode_end,
  2312. .decode = mpeg_decode_frame,
  2313. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
  2314. .flush = flush,
  2315. .max_lowres = 3,
  2316. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2317. };
  2318. #if CONFIG_MPEG_XVMC_DECODER
  2319. static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
  2320. {
  2321. if (avctx->active_thread_type & FF_THREAD_SLICE)
  2322. return -1;
  2323. if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  2324. return -1;
  2325. if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
  2326. av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
  2327. }
  2328. mpeg_decode_init(avctx);
  2329. avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
  2330. avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
  2331. return 0;
  2332. }
  2333. AVCodec ff_mpeg_xvmc_decoder = {
  2334. .name = "mpegvideo_xvmc",
  2335. .type = AVMEDIA_TYPE_VIDEO,
  2336. .id = CODEC_ID_MPEG2VIDEO_XVMC,
  2337. .priv_data_size = sizeof(Mpeg1Context),
  2338. .init = mpeg_mc_decode_init,
  2339. .close = mpeg_decode_end,
  2340. .decode = mpeg_decode_frame,
  2341. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
  2342. CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
  2343. .flush = flush,
  2344. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
  2345. };
  2346. #endif
  2347. #if CONFIG_MPEG_VDPAU_DECODER
  2348. AVCodec ff_mpeg_vdpau_decoder = {
  2349. .name = "mpegvideo_vdpau",
  2350. .type = AVMEDIA_TYPE_VIDEO,
  2351. .id = CODEC_ID_MPEG2VIDEO,
  2352. .priv_data_size = sizeof(Mpeg1Context),
  2353. .init = mpeg_decode_init,
  2354. .close = mpeg_decode_end,
  2355. .decode = mpeg_decode_frame,
  2356. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
  2357. CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
  2358. .flush = flush,
  2359. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
  2360. };
  2361. #endif
  2362. #if CONFIG_MPEG1_VDPAU_DECODER
  2363. AVCodec ff_mpeg1_vdpau_decoder = {
  2364. .name = "mpeg1video_vdpau",
  2365. .type = AVMEDIA_TYPE_VIDEO,
  2366. .id = CODEC_ID_MPEG1VIDEO,
  2367. .priv_data_size = sizeof(Mpeg1Context),
  2368. .init = mpeg_decode_init,
  2369. .close = mpeg_decode_end,
  2370. .decode = mpeg_decode_frame,
  2371. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED |
  2372. CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
  2373. .flush = flush,
  2374. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
  2375. };
  2376. #endif