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.

2775 lines
100KB

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