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.

2660 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(Mpeg1Context *s1, DCTELEM block[12][64])
  661. {
  662. MpegEncContext *s = &s1->mpeg_enc_ctx;
  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 (s1->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. s1->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))
  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. s1->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))
  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. s1->full_pel[0] = s1->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. * decodes a slice. MpegEncContext.mb_y must be set to the MB row from the startcode
  1469. * @return DECODE_SLICE_ERROR if the slice is damaged<br>
  1470. * DECODE_SLICE_OK if this slice is ok<br>
  1471. */
  1472. static int mpeg_decode_slice(Mpeg1Context *s1, int mb_y,
  1473. const uint8_t **buf, int buf_size)
  1474. {
  1475. MpegEncContext *s = &s1->mpeg_enc_ctx;
  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. for (;;) {
  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(s1, 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_BUFFER) && 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((Mpeg1Context*)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, AC_ERROR | DC_ERROR | 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, AC_END | DC_END | 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)
  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->frame_pred_frame_dct = 1;
  1796. s->chroma_format = 1;
  1797. s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG1VIDEO;
  1798. avctx->sub_id = 1; /* indicates MPEG-1 */
  1799. s->out_format = FMT_MPEG1;
  1800. s->swap_uv = 0; // AFAIK VCR2 does not have SEQ_HEADER
  1801. if (s->flags & CODEC_FLAG_LOW_DELAY)
  1802. s->low_delay = 1;
  1803. if (s->avctx->debug & FF_DEBUG_PICT_INFO)
  1804. av_log(s->avctx, AV_LOG_DEBUG, "vbv buffer: %d, bitrate:%d\n",
  1805. s->avctx->rc_buffer_size, s->bit_rate);
  1806. return 0;
  1807. }
  1808. static int vcr2_init_sequence(AVCodecContext *avctx)
  1809. {
  1810. Mpeg1Context *s1 = avctx->priv_data;
  1811. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1812. int i, v;
  1813. /* start new MPEG-1 context decoding */
  1814. s->out_format = FMT_MPEG1;
  1815. if (s1->mpeg_enc_ctx_allocated) {
  1816. MPV_common_end(s);
  1817. }
  1818. s->width = avctx->coded_width;
  1819. s->height = avctx->coded_height;
  1820. avctx->has_b_frames = 0; // true?
  1821. s->low_delay = 1;
  1822. avctx->pix_fmt = mpeg_get_pixelformat(avctx);
  1823. avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
  1824. if( avctx->pix_fmt == PIX_FMT_XVMC_MPEG2_IDCT || avctx->hwaccel )
  1825. if (avctx->idct_algo == FF_IDCT_AUTO)
  1826. avctx->idct_algo = FF_IDCT_SIMPLE;
  1827. if (MPV_common_init(s) < 0)
  1828. return -1;
  1829. exchange_uv(s); // common init reset pblocks, so we swap them here
  1830. s->swap_uv = 1; // in case of xvmc we need to swap uv for each MB
  1831. s1->mpeg_enc_ctx_allocated = 1;
  1832. for (i = 0; i < 64; i++) {
  1833. int j = s->dsp.idct_permutation[i];
  1834. v = ff_mpeg1_default_intra_matrix[i];
  1835. s->intra_matrix[j] = v;
  1836. s->chroma_intra_matrix[j] = v;
  1837. v = ff_mpeg1_default_non_intra_matrix[i];
  1838. s->inter_matrix[j] = v;
  1839. s->chroma_inter_matrix[j] = v;
  1840. }
  1841. s->progressive_sequence = 1;
  1842. s->progressive_frame = 1;
  1843. s->picture_structure = PICT_FRAME;
  1844. s->frame_pred_frame_dct = 1;
  1845. s->chroma_format = 1;
  1846. s->codec_id = s->avctx->codec_id = CODEC_ID_MPEG2VIDEO;
  1847. avctx->sub_id = 2; /* indicates MPEG-2 */
  1848. s1->save_width = s->width;
  1849. s1->save_height = s->height;
  1850. s1->save_progressive_seq = s->progressive_sequence;
  1851. return 0;
  1852. }
  1853. static void mpeg_decode_user_data(AVCodecContext *avctx,
  1854. const uint8_t *p, int buf_size)
  1855. {
  1856. Mpeg1Context *s = avctx->priv_data;
  1857. const uint8_t *buf_end = p + buf_size;
  1858. if(buf_size > 29){
  1859. int i;
  1860. for(i=0; i<20; i++)
  1861. if(!memcmp(p+i, "\0TMPGEXS\0", 9)){
  1862. s->tmpgexs= 1;
  1863. }
  1864. /* for(i=0; !(!p[i-2] && !p[i-1] && p[i]==1) && i<buf_size; i++){
  1865. av_log(0,0, "%c", p[i]);
  1866. }
  1867. av_log(0,0, "\n");*/
  1868. }
  1869. /* we parse the DTG active format information */
  1870. if (buf_end - p >= 5 &&
  1871. p[0] == 'D' && p[1] == 'T' && p[2] == 'G' && p[3] == '1') {
  1872. int flags = p[4];
  1873. p += 5;
  1874. if (flags & 0x80) {
  1875. /* skip event id */
  1876. p += 2;
  1877. }
  1878. if (flags & 0x40) {
  1879. if (buf_end - p < 1)
  1880. return;
  1881. avctx->dtg_active_format = p[0] & 0x0f;
  1882. }
  1883. }
  1884. }
  1885. static void mpeg_decode_gop(AVCodecContext *avctx,
  1886. const uint8_t *buf, int buf_size)
  1887. {
  1888. Mpeg1Context *s1 = avctx->priv_data;
  1889. MpegEncContext *s = &s1->mpeg_enc_ctx;
  1890. int drop_frame_flag;
  1891. int time_code_hours, time_code_minutes;
  1892. int time_code_seconds, time_code_pictures;
  1893. int broken_link;
  1894. init_get_bits(&s->gb, buf, buf_size*8);
  1895. drop_frame_flag = get_bits(&s->gb, 1);
  1896. time_code_hours = get_bits(&s->gb, 5);
  1897. time_code_minutes = get_bits(&s->gb, 6);
  1898. skip_bits1(&s->gb); // marker bit
  1899. time_code_seconds = get_bits(&s->gb, 6);
  1900. time_code_pictures = get_bits(&s->gb, 6);
  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. av_log(s->avctx, AV_LOG_DEBUG, "GOP (%02d:%02d:%02d%c%02d) closed_gop=%d broken_link=%d\n",
  1908. time_code_hours, time_code_minutes, time_code_seconds,
  1909. drop_frame_flag ? ';' : ':',
  1910. time_code_pictures, s->closed_gop, broken_link);
  1911. }
  1912. /**
  1913. * Find the end of the current frame in the bitstream.
  1914. * @return the position of the first byte of the next frame, or -1
  1915. */
  1916. int ff_mpeg1_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size, AVCodecParserContext *s)
  1917. {
  1918. int i;
  1919. uint32_t state = pc->state;
  1920. /* EOF considered as end of frame */
  1921. if (buf_size == 0)
  1922. return 0;
  1923. /*
  1924. 0 frame start -> 1/4
  1925. 1 first_SEQEXT -> 0/2
  1926. 2 first field start -> 3/0
  1927. 3 second_SEQEXT -> 2/0
  1928. 4 searching end
  1929. */
  1930. for (i = 0; i < buf_size; i++) {
  1931. assert(pc->frame_start_found >= 0 && pc->frame_start_found <= 4);
  1932. if (pc->frame_start_found & 1) {
  1933. if (state == EXT_START_CODE && (buf[i] & 0xF0) != 0x80)
  1934. pc->frame_start_found--;
  1935. else if (state == EXT_START_CODE + 2) {
  1936. if ((buf[i] & 3) == 3)
  1937. pc->frame_start_found = 0;
  1938. else
  1939. pc->frame_start_found = (pc->frame_start_found + 1) & 3;
  1940. }
  1941. state++;
  1942. } else {
  1943. i = avpriv_mpv_find_start_code(buf + i, buf + buf_size, &state) - buf - 1;
  1944. if (pc->frame_start_found == 0 && state >= SLICE_MIN_START_CODE && state <= SLICE_MAX_START_CODE) {
  1945. i++;
  1946. pc->frame_start_found = 4;
  1947. }
  1948. if (state == SEQ_END_CODE) {
  1949. pc->state=-1;
  1950. return i+1;
  1951. }
  1952. if (pc->frame_start_found == 2 && state == SEQ_START_CODE)
  1953. pc->frame_start_found = 0;
  1954. if (pc->frame_start_found < 4 && state == EXT_START_CODE)
  1955. pc->frame_start_found++;
  1956. if (pc->frame_start_found == 4 && (state & 0xFFFFFF00) == 0x100) {
  1957. if (state < SLICE_MIN_START_CODE || state > SLICE_MAX_START_CODE) {
  1958. pc->frame_start_found = 0;
  1959. pc->state = -1;
  1960. return i - 3;
  1961. }
  1962. }
  1963. if (pc->frame_start_found == 0 && s && state == PICTURE_START_CODE) {
  1964. ff_fetch_timestamp(s, i - 3, 1);
  1965. }
  1966. }
  1967. }
  1968. pc->state = state;
  1969. return END_NOT_FOUND;
  1970. }
  1971. static int decode_chunks(AVCodecContext *avctx,
  1972. AVFrame *picture, int *data_size,
  1973. const uint8_t *buf, int buf_size);
  1974. /* handle buffering and image synchronisation */
  1975. static int mpeg_decode_frame(AVCodecContext *avctx,
  1976. void *data, int *data_size,
  1977. AVPacket *avpkt)
  1978. {
  1979. const uint8_t *buf = avpkt->data;
  1980. int buf_size = avpkt->size;
  1981. Mpeg1Context *s = avctx->priv_data;
  1982. AVFrame *picture = data;
  1983. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  1984. av_dlog(avctx, "fill_buffer\n");
  1985. if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == SEQ_END_CODE)) {
  1986. /* special case for last picture */
  1987. if (s2->low_delay == 0 && s2->next_picture_ptr) {
  1988. *picture = *(AVFrame*)s2->next_picture_ptr;
  1989. s2->next_picture_ptr = NULL;
  1990. *data_size = sizeof(AVFrame);
  1991. }
  1992. return buf_size;
  1993. }
  1994. if (s2->flags & CODEC_FLAG_TRUNCATED) {
  1995. int next = ff_mpeg1_find_frame_end(&s2->parse_context, buf, buf_size, NULL);
  1996. if (ff_combine_frame(&s2->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0)
  1997. return buf_size;
  1998. }
  1999. if (s->mpeg_enc_ctx_allocated == 0 && avctx->codec_tag == AV_RL32("VCR2"))
  2000. vcr2_init_sequence(avctx);
  2001. s->slice_count = 0;
  2002. if (avctx->extradata && !avctx->frame_number) {
  2003. int ret = decode_chunks(avctx, picture, data_size, avctx->extradata, avctx->extradata_size);
  2004. if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
  2005. return ret;
  2006. }
  2007. return decode_chunks(avctx, picture, data_size, buf, buf_size);
  2008. }
  2009. static int decode_chunks(AVCodecContext *avctx,
  2010. AVFrame *picture, int *data_size,
  2011. const uint8_t *buf, int buf_size)
  2012. {
  2013. Mpeg1Context *s = avctx->priv_data;
  2014. MpegEncContext *s2 = &s->mpeg_enc_ctx;
  2015. const uint8_t *buf_ptr = buf;
  2016. const uint8_t *buf_end = buf + buf_size;
  2017. int ret, input_size;
  2018. int last_code = 0;
  2019. for (;;) {
  2020. /* find next start code */
  2021. uint32_t start_code = -1;
  2022. buf_ptr = avpriv_mpv_find_start_code(buf_ptr, buf_end, &start_code);
  2023. if (start_code > 0x1ff) {
  2024. if (s2->pict_type != AV_PICTURE_TYPE_B || avctx->skip_frame <= AVDISCARD_DEFAULT) {
  2025. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
  2026. int i;
  2027. av_assert0(avctx->thread_count > 1);
  2028. avctx->execute(avctx, slice_decode_thread, &s2->thread_context[0], NULL, s->slice_count, sizeof(void*));
  2029. for (i = 0; i < s->slice_count; i++)
  2030. s2->error_count += s2->thread_context[i]->error_count;
  2031. }
  2032. if (CONFIG_VDPAU && uses_vdpau(avctx))
  2033. ff_vdpau_mpeg_picture_complete(s, buf, buf_size, s->slice_count);
  2034. if (slice_end(avctx, picture)) {
  2035. if (s2->last_picture_ptr || s2->low_delay) //FIXME merge with the stuff in mpeg_decode_slice
  2036. *data_size = sizeof(AVPicture);
  2037. }
  2038. }
  2039. s2->pict_type = 0;
  2040. return FFMAX(0, buf_ptr - buf - s2->parse_context.last_index);
  2041. }
  2042. input_size = buf_end - buf_ptr;
  2043. if (avctx->debug & FF_DEBUG_STARTCODE) {
  2044. av_log(avctx, AV_LOG_DEBUG, "%3X at %td left %d\n", start_code, buf_ptr-buf, input_size);
  2045. }
  2046. /* prepare data for next start code */
  2047. switch (start_code) {
  2048. case SEQ_START_CODE:
  2049. if (last_code == 0) {
  2050. mpeg1_decode_sequence(avctx, buf_ptr, input_size);
  2051. if(buf != avctx->extradata)
  2052. s->sync=1;
  2053. } else {
  2054. av_log(avctx, AV_LOG_ERROR, "ignoring SEQ_START_CODE after %X\n", last_code);
  2055. if (avctx->err_recognition & AV_EF_EXPLODE)
  2056. return AVERROR_INVALIDDATA;
  2057. }
  2058. break;
  2059. case PICTURE_START_CODE:
  2060. if(s->tmpgexs){
  2061. s2->intra_dc_precision= 3;
  2062. s2->intra_matrix[0]= 1;
  2063. }
  2064. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE) && s->slice_count) {
  2065. int i;
  2066. avctx->execute(avctx, slice_decode_thread,
  2067. s2->thread_context, NULL,
  2068. s->slice_count, sizeof(void*));
  2069. for (i = 0; i < s->slice_count; i++)
  2070. s2->error_count += s2->thread_context[i]->error_count;
  2071. s->slice_count = 0;
  2072. }
  2073. if (last_code == 0 || last_code == SLICE_MIN_START_CODE) {
  2074. ret = mpeg_decode_postinit(avctx);
  2075. if (ret < 0) {
  2076. av_log(avctx, AV_LOG_ERROR, "mpeg_decode_postinit() failure\n");
  2077. return ret;
  2078. }
  2079. /* we have a complete image: we try to decompress it */
  2080. if (mpeg1_decode_picture(avctx, buf_ptr, input_size) < 0)
  2081. s2->pict_type = 0;
  2082. s2->first_slice = 1;
  2083. last_code = PICTURE_START_CODE;
  2084. } else {
  2085. av_log(avctx, AV_LOG_ERROR, "ignoring pic after %X\n", last_code);
  2086. if (avctx->err_recognition & AV_EF_EXPLODE)
  2087. return AVERROR_INVALIDDATA;
  2088. }
  2089. break;
  2090. case EXT_START_CODE:
  2091. init_get_bits(&s2->gb, buf_ptr, input_size*8);
  2092. switch (get_bits(&s2->gb, 4)) {
  2093. case 0x1:
  2094. if (last_code == 0) {
  2095. mpeg_decode_sequence_extension(s);
  2096. } else {
  2097. av_log(avctx, AV_LOG_ERROR, "ignoring seq ext after %X\n", last_code);
  2098. if (avctx->err_recognition & AV_EF_EXPLODE)
  2099. return AVERROR_INVALIDDATA;
  2100. }
  2101. break;
  2102. case 0x2:
  2103. mpeg_decode_sequence_display_extension(s);
  2104. break;
  2105. case 0x3:
  2106. mpeg_decode_quant_matrix_extension(s2);
  2107. break;
  2108. case 0x7:
  2109. mpeg_decode_picture_display_extension(s);
  2110. break;
  2111. case 0x8:
  2112. if (last_code == PICTURE_START_CODE) {
  2113. mpeg_decode_picture_coding_extension(s);
  2114. } else {
  2115. av_log(avctx, AV_LOG_ERROR, "ignoring pic cod ext after %X\n", last_code);
  2116. if (avctx->err_recognition & AV_EF_EXPLODE)
  2117. return AVERROR_INVALIDDATA;
  2118. }
  2119. break;
  2120. }
  2121. break;
  2122. case USER_START_CODE:
  2123. mpeg_decode_user_data(avctx, buf_ptr, input_size);
  2124. break;
  2125. case GOP_START_CODE:
  2126. if (last_code == 0) {
  2127. s2->first_field=0;
  2128. mpeg_decode_gop(avctx, buf_ptr, input_size);
  2129. s->sync=1;
  2130. } else {
  2131. av_log(avctx, AV_LOG_ERROR, "ignoring GOP_START_CODE after %X\n", last_code);
  2132. if (avctx->err_recognition & AV_EF_EXPLODE)
  2133. return AVERROR_INVALIDDATA;
  2134. }
  2135. break;
  2136. default:
  2137. if (start_code >= SLICE_MIN_START_CODE &&
  2138. start_code <= SLICE_MAX_START_CODE && last_code != 0) {
  2139. const int field_pic = s2->picture_structure != PICT_FRAME;
  2140. int mb_y = (start_code - SLICE_MIN_START_CODE) << field_pic;
  2141. last_code = SLICE_MIN_START_CODE;
  2142. if (s2->picture_structure == PICT_BOTTOM_FIELD)
  2143. mb_y++;
  2144. if (mb_y >= s2->mb_height) {
  2145. av_log(s2->avctx, AV_LOG_ERROR, "slice below image (%d >= %d)\n", mb_y, s2->mb_height);
  2146. return -1;
  2147. }
  2148. if (s2->last_picture_ptr == NULL) {
  2149. /* Skip B-frames if we do not have reference frames and gop is not closed */
  2150. if (s2->pict_type == AV_PICTURE_TYPE_B) {
  2151. if (!s2->closed_gop)
  2152. break;
  2153. }
  2154. }
  2155. if (s2->pict_type == AV_PICTURE_TYPE_I || (s2->flags2 & CODEC_FLAG2_SHOW_ALL))
  2156. s->sync=1;
  2157. if (s2->next_picture_ptr == NULL) {
  2158. /* Skip P-frames if we do not have a reference frame or we have an invalid header. */
  2159. if (s2->pict_type == AV_PICTURE_TYPE_P && !s->sync) break;
  2160. }
  2161. if ((avctx->skip_frame >= AVDISCARD_NONREF && s2->pict_type == AV_PICTURE_TYPE_B) ||
  2162. (avctx->skip_frame >= AVDISCARD_NONKEY && s2->pict_type != AV_PICTURE_TYPE_I) ||
  2163. avctx->skip_frame >= AVDISCARD_ALL)
  2164. break;
  2165. if (!s->mpeg_enc_ctx_allocated)
  2166. break;
  2167. if (s2->codec_id == CODEC_ID_MPEG2VIDEO) {
  2168. if (mb_y < avctx->skip_top || mb_y >= s2->mb_height - avctx->skip_bottom)
  2169. break;
  2170. }
  2171. if (!s2->pict_type) {
  2172. av_log(avctx, AV_LOG_ERROR, "Missing picture start code\n");
  2173. if (avctx->err_recognition & AV_EF_EXPLODE)
  2174. return AVERROR_INVALIDDATA;
  2175. break;
  2176. }
  2177. if (s2->first_slice) {
  2178. s2->first_slice = 0;
  2179. if (mpeg_field_start(s2, buf, buf_size) < 0)
  2180. return -1;
  2181. }
  2182. if (!s2->current_picture_ptr) {
  2183. av_log(avctx, AV_LOG_ERROR, "current_picture not initialized\n");
  2184. return AVERROR_INVALIDDATA;
  2185. }
  2186. if (uses_vdpau(avctx)) {
  2187. s->slice_count++;
  2188. break;
  2189. }
  2190. if (HAVE_THREADS && (avctx->active_thread_type & FF_THREAD_SLICE)) {
  2191. int threshold= (s2->mb_height*s->slice_count + avctx->thread_count/2) / avctx->thread_count;
  2192. av_assert0(avctx->thread_count > 1);
  2193. if (threshold <= mb_y) {
  2194. MpegEncContext *thread_context = s2->thread_context[s->slice_count];
  2195. thread_context->start_mb_y = mb_y;
  2196. thread_context->end_mb_y = s2->mb_height;
  2197. if (s->slice_count) {
  2198. s2->thread_context[s->slice_count-1]->end_mb_y = mb_y;
  2199. ff_update_duplicate_context(thread_context, s2);
  2200. }
  2201. init_get_bits(&thread_context->gb, buf_ptr, input_size*8);
  2202. s->slice_count++;
  2203. }
  2204. buf_ptr += 2; // FIXME add minimum number of bytes per slice
  2205. } else {
  2206. ret = mpeg_decode_slice(s, mb_y, &buf_ptr, input_size);
  2207. emms_c();
  2208. if (ret < 0) {
  2209. if (avctx->err_recognition & AV_EF_EXPLODE)
  2210. return ret;
  2211. if (s2->resync_mb_x >= 0 && s2->resync_mb_y >= 0)
  2212. 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);
  2213. } else {
  2214. 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);
  2215. }
  2216. }
  2217. }
  2218. break;
  2219. }
  2220. }
  2221. }
  2222. static void flush(AVCodecContext *avctx)
  2223. {
  2224. Mpeg1Context *s = avctx->priv_data;
  2225. s->sync=0;
  2226. ff_mpeg_flush(avctx);
  2227. }
  2228. static int mpeg_decode_end(AVCodecContext *avctx)
  2229. {
  2230. Mpeg1Context *s = avctx->priv_data;
  2231. if (s->mpeg_enc_ctx_allocated)
  2232. MPV_common_end(&s->mpeg_enc_ctx);
  2233. return 0;
  2234. }
  2235. static const AVProfile mpeg2_video_profiles[] = {
  2236. { FF_PROFILE_MPEG2_422, "4:2:2" },
  2237. { FF_PROFILE_MPEG2_HIGH, "High" },
  2238. { FF_PROFILE_MPEG2_SS, "Spatially Scalable" },
  2239. { FF_PROFILE_MPEG2_SNR_SCALABLE, "SNR Scalable" },
  2240. { FF_PROFILE_MPEG2_MAIN, "Main" },
  2241. { FF_PROFILE_MPEG2_SIMPLE, "Simple" },
  2242. { FF_PROFILE_RESERVED, "Reserved" },
  2243. { FF_PROFILE_RESERVED, "Reserved" },
  2244. { FF_PROFILE_UNKNOWN },
  2245. };
  2246. AVCodec ff_mpeg1video_decoder = {
  2247. .name = "mpeg1video",
  2248. .type = AVMEDIA_TYPE_VIDEO,
  2249. .id = CODEC_ID_MPEG1VIDEO,
  2250. .priv_data_size = sizeof(Mpeg1Context),
  2251. .init = mpeg_decode_init,
  2252. .close = mpeg_decode_end,
  2253. .decode = mpeg_decode_frame,
  2254. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
  2255. .flush = flush,
  2256. .max_lowres = 3,
  2257. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2258. .update_thread_context = ONLY_IF_THREADS_ENABLED(mpeg_decode_update_thread_context)
  2259. };
  2260. AVCodec ff_mpeg2video_decoder = {
  2261. .name = "mpeg2video",
  2262. .type = AVMEDIA_TYPE_VIDEO,
  2263. .id = CODEC_ID_MPEG2VIDEO,
  2264. .priv_data_size = sizeof(Mpeg1Context),
  2265. .init = mpeg_decode_init,
  2266. .close = mpeg_decode_end,
  2267. .decode = mpeg_decode_frame,
  2268. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
  2269. .flush = flush,
  2270. .max_lowres = 3,
  2271. .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 video"),
  2272. .profiles = NULL_IF_CONFIG_SMALL(mpeg2_video_profiles),
  2273. };
  2274. //legacy decoder
  2275. AVCodec ff_mpegvideo_decoder = {
  2276. .name = "mpegvideo",
  2277. .type = AVMEDIA_TYPE_VIDEO,
  2278. .id = CODEC_ID_MPEG2VIDEO,
  2279. .priv_data_size = sizeof(Mpeg1Context),
  2280. .init = mpeg_decode_init,
  2281. .close = mpeg_decode_end,
  2282. .decode = mpeg_decode_frame,
  2283. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY | CODEC_CAP_SLICE_THREADS,
  2284. .flush = flush,
  2285. .max_lowres = 3,
  2286. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video"),
  2287. };
  2288. #if CONFIG_MPEG_XVMC_DECODER
  2289. static av_cold int mpeg_mc_decode_init(AVCodecContext *avctx)
  2290. {
  2291. if (avctx->active_thread_type & FF_THREAD_SLICE)
  2292. return -1;
  2293. if (!(avctx->slice_flags & SLICE_FLAG_CODED_ORDER))
  2294. return -1;
  2295. if (!(avctx->slice_flags & SLICE_FLAG_ALLOW_FIELD)) {
  2296. av_dlog(avctx, "mpeg12.c: XvMC decoder will work better if SLICE_FLAG_ALLOW_FIELD is set\n");
  2297. }
  2298. mpeg_decode_init(avctx);
  2299. avctx->pix_fmt = PIX_FMT_XVMC_MPEG2_IDCT;
  2300. avctx->xvmc_acceleration = 2; // 2 - the blocks are packed!
  2301. return 0;
  2302. }
  2303. AVCodec ff_mpeg_xvmc_decoder = {
  2304. .name = "mpegvideo_xvmc",
  2305. .type = AVMEDIA_TYPE_VIDEO,
  2306. .id = CODEC_ID_MPEG2VIDEO_XVMC,
  2307. .priv_data_size = sizeof(Mpeg1Context),
  2308. .init = mpeg_mc_decode_init,
  2309. .close = mpeg_decode_end,
  2310. .decode = mpeg_decode_frame,
  2311. .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED| CODEC_CAP_HWACCEL | CODEC_CAP_DELAY,
  2312. .flush = flush,
  2313. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video XvMC (X-Video Motion Compensation)"),
  2314. };
  2315. #endif
  2316. #if CONFIG_MPEG_VDPAU_DECODER
  2317. AVCodec ff_mpeg_vdpau_decoder = {
  2318. .name = "mpegvideo_vdpau",
  2319. .type = AVMEDIA_TYPE_VIDEO,
  2320. .id = CODEC_ID_MPEG2VIDEO,
  2321. .priv_data_size = sizeof(Mpeg1Context),
  2322. .init = mpeg_decode_init,
  2323. .close = mpeg_decode_end,
  2324. .decode = mpeg_decode_frame,
  2325. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
  2326. .flush = flush,
  2327. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1/2 video (VDPAU acceleration)"),
  2328. };
  2329. #endif
  2330. #if CONFIG_MPEG1_VDPAU_DECODER
  2331. AVCodec ff_mpeg1_vdpau_decoder = {
  2332. .name = "mpeg1video_vdpau",
  2333. .type = AVMEDIA_TYPE_VIDEO,
  2334. .id = CODEC_ID_MPEG1VIDEO,
  2335. .priv_data_size = sizeof(Mpeg1Context),
  2336. .init = mpeg_decode_init,
  2337. .close = mpeg_decode_end,
  2338. .decode = mpeg_decode_frame,
  2339. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_HWACCEL_VDPAU | CODEC_CAP_DELAY,
  2340. .flush = flush,
  2341. .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 video (VDPAU acceleration)"),
  2342. };
  2343. #endif