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.

2672 lines
95KB

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