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.

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