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.

2594 lines
94KB

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