rv10.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /*
  2. * RV10/RV20 decoder
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer
  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. * RV10/RV20 decoder
  25. */
  26. #include <inttypes.h>
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/thread.h"
  29. #include "avcodec.h"
  30. #include "codec_internal.h"
  31. #include "decode.h"
  32. #include "error_resilience.h"
  33. #include "h263.h"
  34. #include "h263data.h"
  35. #include "h263dec.h"
  36. #include "mpeg_er.h"
  37. #include "mpegvideo.h"
  38. #include "mpegvideodec.h"
  39. #include "mpeg4video.h"
  40. #include "mpegvideodata.h"
  41. #include "rv10dec.h"
  42. #define RV_GET_MAJOR_VER(x) ((x) >> 28)
  43. #define RV_GET_MINOR_VER(x) (((x) >> 20) & 0xFF)
  44. #define RV_GET_MICRO_VER(x) (((x) >> 12) & 0xFF)
  45. #define MAX_VLC_ENTRIES 1023 // Note: Does not include the skip entries.
  46. #define DC_VLC_BITS 9
  47. typedef struct RVDecContext {
  48. H263DecContext h;
  49. int sub_id;
  50. int orig_width, orig_height;
  51. } RVDecContext;
  52. /* (run, length) encoded value for the symbols table. The actual symbols
  53. * are run..run - length (mod 256).
  54. * The last two entries in the following table apply to luma only.
  55. * The skip values are not included in this list. */
  56. static const uint8_t rv_sym_run_len[][2] = {
  57. { 0, 0 }, { 1, 0 }, { 255, 0 }, { 3, 1 }, { 254, 1 },
  58. { 7, 3 }, { 252, 3 }, { 15, 7 }, { 248, 7 }, { 31, 15 },
  59. { 240, 15 }, { 63, 31 }, { 224, 31 }, { 127, 63 }, { 192, 63 },
  60. { 255, 127 }, { 128, 127 }, { 127, 255 }, { 128, 255 },
  61. };
  62. /* entry[i] of the following tables gives
  63. * the number of VLC codes of length i + 2. */
  64. static const uint16_t rv_lum_len_count[15] = {
  65. 1, 0, 2, 4, 8, 16, 32, 0, 64, 0, 128, 0, 256, 0, 512,
  66. };
  67. static const uint16_t rv_chrom_len_count[15] = {
  68. 1, 2, 4, 0, 8, 0, 16, 0, 32, 0, 64, 0, 128, 0, 256,
  69. };
  70. static VLCElem rv_dc_lum[1472], rv_dc_chrom[992];
  71. int ff_rv_decode_dc(H263DecContext *const h, int n)
  72. {
  73. int code;
  74. if (n < 4) {
  75. code = get_vlc2(&h->gb, rv_dc_lum, DC_VLC_BITS, 2);
  76. } else {
  77. code = get_vlc2(&h->gb, rv_dc_chrom, DC_VLC_BITS, 2);
  78. if (code < 0) {
  79. av_log(h->c.avctx, AV_LOG_ERROR, "chroma dc error\n");
  80. return -1;
  81. }
  82. }
  83. return code;
  84. }
  85. /* read RV 1.0 compatible frame header */
  86. static int rv10_decode_picture_header(H263DecContext *const h)
  87. {
  88. int mb_count, pb_frame, marker, mb_xy;
  89. marker = get_bits1(&h->gb);
  90. if (get_bits1(&h->gb))
  91. h->c.pict_type = AV_PICTURE_TYPE_P;
  92. else
  93. h->c.pict_type = AV_PICTURE_TYPE_I;
  94. if (!marker)
  95. av_log(h->c.avctx, AV_LOG_ERROR, "marker missing\n");
  96. pb_frame = get_bits1(&h->gb);
  97. ff_dlog(h->c.avctx, "pict_type=%d pb_frame=%d\n", h->c.pict_type, pb_frame);
  98. if (pb_frame) {
  99. avpriv_request_sample(h->c.avctx, "PB-frame");
  100. return AVERROR_PATCHWELCOME;
  101. }
  102. h->c.qscale = get_bits(&h->gb, 5);
  103. if (h->c.qscale == 0) {
  104. av_log(h->c.avctx, AV_LOG_ERROR, "Invalid qscale value: 0\n");
  105. return AVERROR_INVALIDDATA;
  106. }
  107. if (h->c.pict_type == AV_PICTURE_TYPE_I) {
  108. if (h->rv10_version == 3) {
  109. /* specific MPEG like DC coding not used */
  110. h->last_dc[0] = get_bits(&h->gb, 8);
  111. h->last_dc[1] = get_bits(&h->gb, 8);
  112. h->last_dc[2] = get_bits(&h->gb, 8);
  113. ff_dlog(h->c.avctx, "DC:%d %d %d\n", h->last_dc[0],
  114. h->last_dc[1], h->last_dc[2]);
  115. }
  116. }
  117. /* if multiple packets per frame are sent, the position at which
  118. * to display the macroblocks is coded here */
  119. mb_xy = h->c.mb_x + h->c.mb_y * h->c.mb_width;
  120. if (show_bits(&h->gb, 12) == 0 || (mb_xy && mb_xy < h->c.mb_num)) {
  121. h->c.mb_x = get_bits(&h->gb, 6); /* mb_x */
  122. h->c.mb_y = get_bits(&h->gb, 6); /* mb_y */
  123. mb_count = get_bits(&h->gb, 12);
  124. } else {
  125. h->c.mb_x = 0;
  126. h->c.mb_y = 0;
  127. mb_count = h->c.mb_width * h->c.mb_height;
  128. }
  129. skip_bits(&h->gb, 3); /* ignored */
  130. return mb_count;
  131. }
  132. static int rv20_decode_picture_header(RVDecContext *rv, int whole_size)
  133. {
  134. static const enum AVPictureType pict_types[] =
  135. { AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_I /* hmm ... */,
  136. AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B };
  137. H263DecContext *const h = &rv->h;
  138. int seq, mb_pos, ret;
  139. int rpr_max;
  140. h->c.pict_type = pict_types[get_bits(&h->gb, 2)];
  141. if (h->c.low_delay && h->c.pict_type == AV_PICTURE_TYPE_B) {
  142. av_log(h->c.avctx, AV_LOG_ERROR, "low delay B\n");
  143. return -1;
  144. }
  145. if (!h->c.last_pic.ptr && h->c.pict_type == AV_PICTURE_TYPE_B) {
  146. av_log(h->c.avctx, AV_LOG_ERROR, "early B-frame\n");
  147. return AVERROR_INVALIDDATA;
  148. }
  149. if (get_bits1(&h->gb)) {
  150. av_log(h->c.avctx, AV_LOG_ERROR, "reserved bit set\n");
  151. return AVERROR_INVALIDDATA;
  152. }
  153. h->c.qscale = get_bits(&h->gb, 5);
  154. if (h->c.qscale == 0) {
  155. av_log(h->c.avctx, AV_LOG_ERROR, "Invalid qscale value: 0\n");
  156. return AVERROR_INVALIDDATA;
  157. }
  158. if (RV_GET_MINOR_VER(rv->sub_id) >= 2)
  159. h->loop_filter = get_bits1(&h->gb) && !h->c.avctx->lowres;
  160. if (RV_GET_MINOR_VER(rv->sub_id) <= 1)
  161. seq = get_bits(&h->gb, 8) << 7;
  162. else
  163. seq = get_bits(&h->gb, 13) << 2;
  164. rpr_max = h->c.avctx->extradata[1] & 7;
  165. if (rpr_max) {
  166. int f, new_w, new_h;
  167. int rpr_bits = av_log2(rpr_max) + 1;
  168. f = get_bits(&h->gb, rpr_bits);
  169. if (f) {
  170. if (h->c.avctx->extradata_size < 8 + 2 * f) {
  171. av_log(h->c.avctx, AV_LOG_ERROR, "Extradata too small.\n");
  172. return AVERROR_INVALIDDATA;
  173. }
  174. new_w = 4 * h->c.avctx->extradata[6 + 2 * f];
  175. new_h = 4 * h->c.avctx->extradata[7 + 2 * f];
  176. } else {
  177. new_w = rv->orig_width;
  178. new_h = rv->orig_height;
  179. }
  180. if (new_w != h->c.width || new_h != h->c.height || !h->c.context_initialized) {
  181. AVRational old_aspect = h->c.avctx->sample_aspect_ratio;
  182. av_log(h->c.avctx, AV_LOG_DEBUG,
  183. "attempting to change resolution to %dx%d\n", new_w, new_h);
  184. if (av_image_check_size(new_w, new_h, 0, h->c.avctx) < 0)
  185. return AVERROR_INVALIDDATA;
  186. if (whole_size < (new_w + 15)/16 * ((new_h + 15)/16) / 8)
  187. return AVERROR_INVALIDDATA;
  188. ff_mpv_common_end(&h->c);
  189. // attempt to keep aspect during typical resolution switches
  190. if (!old_aspect.num)
  191. old_aspect = (AVRational){1, 1};
  192. if (2 * (int64_t)new_w * h->c.height == (int64_t)new_h * h->c.width)
  193. h->c.avctx->sample_aspect_ratio = av_mul_q(old_aspect, (AVRational){2, 1});
  194. if ((int64_t)new_w * h->c.height == 2 * (int64_t)new_h * h->c.width)
  195. h->c.avctx->sample_aspect_ratio = av_mul_q(old_aspect, (AVRational){1, 2});
  196. ret = ff_set_dimensions(h->c.avctx, new_w, new_h);
  197. if (ret < 0)
  198. return ret;
  199. h->c.width = new_w;
  200. h->c.height = new_h;
  201. if ((ret = ff_mpv_common_init(&h->c)) < 0)
  202. return ret;
  203. }
  204. if (h->c.avctx->debug & FF_DEBUG_PICT_INFO) {
  205. av_log(h->c.avctx, AV_LOG_DEBUG, "F %d/%d/%d\n", f, rpr_bits, rpr_max);
  206. }
  207. }
  208. if (av_image_check_size(h->c.width, h->c.height, 0, h->c.avctx) < 0)
  209. return AVERROR_INVALIDDATA;
  210. mb_pos = ff_h263_decode_mba(h);
  211. seq |= h->c.time & ~0x7FFF;
  212. if (seq - h->c.time > 0x4000)
  213. seq -= 0x8000;
  214. if (seq - h->c.time < -0x4000)
  215. seq += 0x8000;
  216. if (seq != h->c.time) {
  217. if (h->c.pict_type != AV_PICTURE_TYPE_B) {
  218. h->c.time = seq;
  219. h->c.pp_time = h->c.time - h->c.last_non_b_time;
  220. h->c.last_non_b_time = h->c.time;
  221. } else {
  222. h->c.time = seq;
  223. h->c.pb_time = h->c.pp_time - (h->c.last_non_b_time - h->c.time);
  224. }
  225. }
  226. if (h->c.pict_type == AV_PICTURE_TYPE_B) {
  227. if (h->c.pp_time <=h->c.pb_time || h->c.pp_time <= h->c.pp_time - h->c.pb_time || h->c.pp_time<=0) {
  228. av_log(h->c.avctx, AV_LOG_DEBUG,
  229. "messed up order, possible from seeking? skipping current B-frame\n");
  230. #define ERROR_SKIP_FRAME -123
  231. return ERROR_SKIP_FRAME;
  232. }
  233. ff_mpeg4_init_direct_mv(&h->c);
  234. }
  235. h->c.no_rounding = get_bits1(&h->gb);
  236. if (RV_GET_MINOR_VER(rv->sub_id) <= 1 && h->c.pict_type == AV_PICTURE_TYPE_B)
  237. // binary decoder reads 3+2 bits here but they don't seem to be used
  238. skip_bits(&h->gb, 5);
  239. h->c.h263_aic = h->c.pict_type == AV_PICTURE_TYPE_I;
  240. if (h->c.h263_aic) {
  241. h->c.y_dc_scale_table =
  242. h->c.c_dc_scale_table = ff_aic_dc_scale_table;
  243. } else {
  244. h->c.y_dc_scale_table =
  245. h->c.c_dc_scale_table = ff_mpeg1_dc_scale_table;
  246. }
  247. if (!h->c.avctx->lowres)
  248. h->loop_filter = 1;
  249. if (h->c.avctx->debug & FF_DEBUG_PICT_INFO) {
  250. av_log(h->c.avctx, AV_LOG_INFO,
  251. "num:%5d x:%2d y:%2d type:%d qscale:%2d rnd:%d\n",
  252. seq, h->c.mb_x, h->c.mb_y, h->c.pict_type, h->c.qscale,
  253. h->c.no_rounding);
  254. }
  255. av_assert0(h->c.pict_type != AV_PICTURE_TYPE_B || !h->c.low_delay);
  256. return h->c.mb_width * h->c.mb_height - mb_pos;
  257. }
  258. static av_cold void rv10_build_vlc(VLCElem vlc[], int table_size,
  259. const uint16_t len_count[15],
  260. const uint8_t sym_rl[][2], int sym_rl_elems)
  261. {
  262. uint16_t syms[MAX_VLC_ENTRIES];
  263. uint8_t lens[MAX_VLC_ENTRIES];
  264. unsigned nb_syms = 0, nb_lens = 0;
  265. for (unsigned i = 0; i < sym_rl_elems; i++) {
  266. unsigned cur_sym = sym_rl[i][0];
  267. for (unsigned tmp = nb_syms + sym_rl[i][1]; nb_syms <= tmp; nb_syms++)
  268. syms[nb_syms] = 0xFF & cur_sym--;
  269. }
  270. for (unsigned i = 0; i < 15; i++)
  271. for (unsigned tmp = nb_lens + len_count[i]; nb_lens < tmp; nb_lens++)
  272. lens[nb_lens] = i + 2;
  273. av_assert1(nb_lens == nb_syms);
  274. ff_vlc_init_table_from_lengths(vlc, table_size, DC_VLC_BITS, nb_lens,
  275. lens, 1, syms, 2, 2, 0, 0);
  276. }
  277. static av_cold void rv10_init_static(void)
  278. {
  279. rv10_build_vlc(rv_dc_lum, FF_ARRAY_ELEMS(rv_dc_lum), rv_lum_len_count,
  280. rv_sym_run_len, FF_ARRAY_ELEMS(rv_sym_run_len));
  281. for (int i = 0; i < 1 << (DC_VLC_BITS - 7 /* Length of skip prefix */); i++) {
  282. /* All codes beginning with 0x7F have the same length and value.
  283. * Modifying the table directly saves us the useless subtables. */
  284. rv_dc_lum[(0x7F << (DC_VLC_BITS - 7)) + i].sym = 255;
  285. rv_dc_lum[(0x7F << (DC_VLC_BITS - 7)) + i].len = 18;
  286. }
  287. rv10_build_vlc(rv_dc_chrom, FF_ARRAY_ELEMS(rv_dc_chrom), rv_chrom_len_count,
  288. rv_sym_run_len, FF_ARRAY_ELEMS(rv_sym_run_len) - 2);
  289. for (int i = 0; i < 1 << (DC_VLC_BITS - 9 /* Length of skip prefix */); i++) {
  290. /* Same as above. */
  291. rv_dc_chrom[(0x1FE << (DC_VLC_BITS - 9)) + i].sym = 255;
  292. rv_dc_chrom[(0x1FE << (DC_VLC_BITS - 9)) + i].len = 18;
  293. }
  294. }
  295. static av_cold int rv10_decode_init(AVCodecContext *avctx)
  296. {
  297. static AVOnce init_static_once = AV_ONCE_INIT;
  298. RVDecContext *rv = avctx->priv_data;
  299. H263DecContext *const h = &rv->h;
  300. int major_ver, minor_ver, micro_ver, ret;
  301. if (avctx->extradata_size < 8) {
  302. av_log(avctx, AV_LOG_ERROR, "Extradata is too small.\n");
  303. return AVERROR_INVALIDDATA;
  304. }
  305. if ((ret = av_image_check_size(avctx->coded_width,
  306. avctx->coded_height, 0, avctx)) < 0)
  307. return ret;
  308. ret = ff_h263_decode_init(avctx);
  309. if (ret < 0)
  310. return ret;
  311. rv->orig_width = avctx->coded_width;
  312. rv->orig_height = avctx->coded_height;
  313. h->h263_long_vectors = avctx->extradata[3] & 1;
  314. rv->sub_id = AV_RB32A(avctx->extradata + 4);
  315. if (avctx->codec_id == AV_CODEC_ID_RV20) {
  316. h->modified_quant = 1;
  317. h->c.chroma_qscale_table = ff_h263_chroma_qscale_table;
  318. }
  319. major_ver = RV_GET_MAJOR_VER(rv->sub_id);
  320. minor_ver = RV_GET_MINOR_VER(rv->sub_id);
  321. micro_ver = RV_GET_MICRO_VER(rv->sub_id);
  322. switch (major_ver) {
  323. case 1:
  324. h->rv10_version = micro_ver ? 3 : 1;
  325. h->c.obmc = micro_ver == 2;
  326. break;
  327. case 2:
  328. if (minor_ver >= 2) {
  329. h->c.low_delay = 0;
  330. avctx->has_b_frames = 1;
  331. }
  332. break;
  333. default:
  334. av_log(avctx, AV_LOG_ERROR, "unknown header %X\n", rv->sub_id);
  335. avpriv_request_sample(avctx, "RV1/2 version");
  336. return AVERROR_PATCHWELCOME;
  337. }
  338. if (avctx->debug & FF_DEBUG_PICT_INFO) {
  339. av_log(avctx, AV_LOG_DEBUG, "ver:%X ver0:%"PRIX32"\n", rv->sub_id,
  340. AV_RL32A(avctx->extradata));
  341. }
  342. /* init static VLCs */
  343. ff_thread_once(&init_static_once, rv10_init_static);
  344. return 0;
  345. }
  346. static int rv10_decode_packet(AVCodecContext *avctx, const uint8_t *buf,
  347. int buf_size, int buf_size2, int whole_size)
  348. {
  349. RVDecContext *rv = avctx->priv_data;
  350. H263DecContext *const h = &rv->h;
  351. int mb_count, mb_pos, left, start_mb_x, active_bits_size, ret;
  352. active_bits_size = buf_size * 8;
  353. init_get_bits(&h->gb, buf, FFMAX(buf_size, buf_size2) * 8);
  354. if (h->c.codec_id == AV_CODEC_ID_RV10)
  355. mb_count = rv10_decode_picture_header(h);
  356. else
  357. mb_count = rv20_decode_picture_header(rv, whole_size);
  358. if (mb_count < 0) {
  359. if (mb_count != ERROR_SKIP_FRAME)
  360. av_log(h->c.avctx, AV_LOG_ERROR, "HEADER ERROR\n");
  361. return AVERROR_INVALIDDATA;
  362. }
  363. if (h->c.mb_x >= h->c.mb_width ||
  364. h->c.mb_y >= h->c.mb_height) {
  365. av_log(h->c.avctx, AV_LOG_ERROR, "POS ERROR %d %d\n", h->c.mb_x, h->c.mb_y);
  366. return AVERROR_INVALIDDATA;
  367. }
  368. mb_pos = h->c.mb_y * h->c.mb_width + h->c.mb_x;
  369. left = h->c.mb_width * h->c.mb_height - mb_pos;
  370. if (mb_count > left) {
  371. av_log(h->c.avctx, AV_LOG_ERROR, "COUNT ERROR\n");
  372. return AVERROR_INVALIDDATA;
  373. }
  374. if (whole_size < h->c.mb_width * h->c.mb_height / 8)
  375. return AVERROR_INVALIDDATA;
  376. if ((h->c.mb_x == 0 && h->c.mb_y == 0) || !h->c.cur_pic.ptr) {
  377. // FIXME write parser so we always have complete frames?
  378. if (h->c.cur_pic.ptr) {
  379. ff_er_frame_end(&h->c.er, NULL);
  380. ff_mpv_frame_end(&h->c);
  381. h->c.mb_x = h->c.mb_y = h->c.resync_mb_x = h->c.resync_mb_y = 0;
  382. }
  383. if ((ret = ff_mpv_frame_start(&h->c, avctx)) < 0)
  384. return ret;
  385. ff_mpv_er_frame_start_ext(&h->c, 0, h->c.pp_time, h->c.pb_time);
  386. } else {
  387. if (h->c.cur_pic.ptr->f->pict_type != h->c.pict_type) {
  388. av_log(h->c.avctx, AV_LOG_ERROR, "Slice type mismatch\n");
  389. return AVERROR_INVALIDDATA;
  390. }
  391. }
  392. ff_dlog(avctx, "qscale=%d\n", h->c.qscale);
  393. /* default quantization values */
  394. if (h->c.codec_id == AV_CODEC_ID_RV10) {
  395. if (h->c.mb_y == 0)
  396. h->c.first_slice_line = 1;
  397. } else {
  398. h->c.first_slice_line = 1;
  399. h->c.resync_mb_x = h->c.mb_x;
  400. }
  401. start_mb_x = h->c.mb_x;
  402. h->c.resync_mb_y = h->c.mb_y;
  403. ff_set_qscale(&h->c, h->c.qscale);
  404. h->rv10_first_dc_coded[0] = 0;
  405. h->rv10_first_dc_coded[1] = 0;
  406. h->rv10_first_dc_coded[2] = 0;
  407. ff_init_block_index(&h->c);
  408. /* decode each macroblock */
  409. for (h->mb_num_left = mb_count; h->mb_num_left > 0; h->mb_num_left--) {
  410. int ret;
  411. ff_update_block_index(&h->c, 8, h->c.avctx->lowres, 1);
  412. ff_tlog(avctx, "**mb x=%d y=%d\n", h->c.mb_x, h->c.mb_y);
  413. h->c.mv_dir = MV_DIR_FORWARD;
  414. h->c.mv_type = MV_TYPE_16X16;
  415. ret = ff_h263_decode_mb(h);
  416. // Repeat the slice end check from ff_h263_decode_mb with our active
  417. // bitstream size
  418. if (ret != SLICE_ERROR && active_bits_size >= get_bits_count(&h->gb)) {
  419. int v = show_bits(&h->gb, 16);
  420. if (get_bits_count(&h->gb) + 16 > active_bits_size)
  421. v >>= get_bits_count(&h->gb) + 16 - active_bits_size;
  422. if (!v)
  423. ret = SLICE_END;
  424. }
  425. if (ret != SLICE_ERROR && active_bits_size < get_bits_count(&h->gb) &&
  426. 8 * buf_size2 >= get_bits_count(&h->gb)) {
  427. active_bits_size = buf_size2 * 8;
  428. av_log(avctx, AV_LOG_DEBUG, "update size from %d to %d\n",
  429. 8 * buf_size, active_bits_size);
  430. ret = SLICE_OK;
  431. }
  432. if (ret == SLICE_ERROR || active_bits_size < get_bits_count(&h->gb)) {
  433. av_log(h->c.avctx, AV_LOG_ERROR, "ERROR at MB %d %d\n", h->c.mb_x,
  434. h->c.mb_y);
  435. return AVERROR_INVALIDDATA;
  436. }
  437. if (h->c.pict_type != AV_PICTURE_TYPE_B)
  438. ff_h263_update_motion_val(&h->c);
  439. ff_mpv_reconstruct_mb(&h->c, h->block);
  440. if (h->loop_filter)
  441. ff_h263_loop_filter(&h->c);
  442. if (++h->c.mb_x == h->c.mb_width) {
  443. h->c.mb_x = 0;
  444. h->c.mb_y++;
  445. ff_init_block_index(&h->c);
  446. }
  447. if (h->c.mb_x == h->c.resync_mb_x)
  448. h->c.first_slice_line = 0;
  449. if (ret == SLICE_END)
  450. break;
  451. }
  452. ff_er_add_slice(&h->c.er, start_mb_x, h->c.resync_mb_y, h->c.mb_x - 1, h->c.mb_y,
  453. ER_MB_END);
  454. return active_bits_size;
  455. }
  456. static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n)
  457. {
  458. return AV_RL32(buf + n * 8);
  459. }
  460. static int rv10_decode_frame(AVCodecContext *avctx, AVFrame *pict,
  461. int *got_frame, AVPacket *avpkt)
  462. {
  463. const uint8_t *buf = avpkt->data;
  464. int buf_size = avpkt->size;
  465. MpegEncContext *s = avctx->priv_data;
  466. int i, ret;
  467. int slice_count;
  468. const uint8_t *slices_hdr = NULL;
  469. ff_dlog(avctx, "*****frame %"PRId64" size=%d\n", avctx->frame_num, buf_size);
  470. /* no supplementary picture */
  471. if (buf_size == 0) {
  472. return 0;
  473. }
  474. slice_count = (*buf++) + 1;
  475. buf_size--;
  476. if (!slice_count || buf_size <= 8 * slice_count) {
  477. av_log(avctx, AV_LOG_ERROR, "Invalid slice count: %d.\n",
  478. slice_count);
  479. return AVERROR_INVALIDDATA;
  480. }
  481. slices_hdr = buf + 4;
  482. buf += 8 * slice_count;
  483. buf_size -= 8 * slice_count;
  484. for (i = 0; i < slice_count; i++) {
  485. unsigned offset = get_slice_offset(avctx, slices_hdr, i);
  486. int size, size2;
  487. if (offset >= buf_size)
  488. return AVERROR_INVALIDDATA;
  489. if (i + 1 == slice_count)
  490. size = buf_size - offset;
  491. else
  492. size = get_slice_offset(avctx, slices_hdr, i + 1) - offset;
  493. if (i + 2 >= slice_count)
  494. size2 = buf_size - offset;
  495. else
  496. size2 = get_slice_offset(avctx, slices_hdr, i + 2) - offset;
  497. if (size <= 0 || size2 <= 0 ||
  498. offset + FFMAX(size, size2) > buf_size)
  499. return AVERROR_INVALIDDATA;
  500. if ((ret = rv10_decode_packet(avctx, buf + offset, size, size2, buf_size)) < 0)
  501. return ret;
  502. if (ret > 8 * size)
  503. i++;
  504. }
  505. if (s->cur_pic.ptr && s->mb_y >= s->mb_height) {
  506. ff_er_frame_end(&s->er, NULL);
  507. ff_mpv_frame_end(s);
  508. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  509. if ((ret = av_frame_ref(pict, s->cur_pic.ptr->f)) < 0)
  510. return ret;
  511. ff_print_debug_info(s, s->cur_pic.ptr, pict);
  512. ff_mpv_export_qp_table(s, pict, s->cur_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG1);
  513. } else if (s->last_pic.ptr) {
  514. if ((ret = av_frame_ref(pict, s->last_pic.ptr->f)) < 0)
  515. return ret;
  516. ff_print_debug_info(s, s->last_pic.ptr, pict);
  517. ff_mpv_export_qp_table(s, pict,s->last_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG1);
  518. }
  519. if (s->last_pic.ptr || s->low_delay) {
  520. *got_frame = 1;
  521. }
  522. // so we can detect if frame_end was not called (find some nicer solution...)
  523. ff_mpv_unref_picture(&s->cur_pic);
  524. }
  525. return avpkt->size;
  526. }
  527. const FFCodec ff_rv10_decoder = {
  528. .p.name = "rv10",
  529. CODEC_LONG_NAME("RealVideo 1.0"),
  530. .p.type = AVMEDIA_TYPE_VIDEO,
  531. .p.id = AV_CODEC_ID_RV10,
  532. .priv_data_size = sizeof(RVDecContext),
  533. .init = rv10_decode_init,
  534. FF_CODEC_DECODE_CB(rv10_decode_frame),
  535. .close = ff_mpv_decode_close,
  536. .p.capabilities = AV_CODEC_CAP_DR1,
  537. .p.max_lowres = 3,
  538. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  539. };
  540. const FFCodec ff_rv20_decoder = {
  541. .p.name = "rv20",
  542. CODEC_LONG_NAME("RealVideo 2.0"),
  543. .p.type = AVMEDIA_TYPE_VIDEO,
  544. .p.id = AV_CODEC_ID_RV20,
  545. .priv_data_size = sizeof(RVDecContext),
  546. .init = rv10_decode_init,
  547. FF_CODEC_DECODE_CB(rv10_decode_frame),
  548. .close = ff_mpv_decode_close,
  549. .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
  550. .flush = ff_mpeg_flush,
  551. .p.max_lowres = 3,
  552. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
  553. };