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.

2596 lines
93KB

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