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.

2730 lines
98KB

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