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.

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