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.

803 lines
26KB

  1. /*
  2. * RV10 codec
  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 libavcodec/rv10.c
  24. * RV10 codec.
  25. */
  26. #include "avcodec.h"
  27. #include "dsputil.h"
  28. #include "mpegvideo.h"
  29. //#define DEBUG
  30. #define DC_VLC_BITS 14 //FIXME find a better solution
  31. static const uint16_t rv_lum_code[256] =
  32. {
  33. 0x3e7f, 0x0f00, 0x0f01, 0x0f02, 0x0f03, 0x0f04, 0x0f05, 0x0f06,
  34. 0x0f07, 0x0f08, 0x0f09, 0x0f0a, 0x0f0b, 0x0f0c, 0x0f0d, 0x0f0e,
  35. 0x0f0f, 0x0f10, 0x0f11, 0x0f12, 0x0f13, 0x0f14, 0x0f15, 0x0f16,
  36. 0x0f17, 0x0f18, 0x0f19, 0x0f1a, 0x0f1b, 0x0f1c, 0x0f1d, 0x0f1e,
  37. 0x0f1f, 0x0f20, 0x0f21, 0x0f22, 0x0f23, 0x0f24, 0x0f25, 0x0f26,
  38. 0x0f27, 0x0f28, 0x0f29, 0x0f2a, 0x0f2b, 0x0f2c, 0x0f2d, 0x0f2e,
  39. 0x0f2f, 0x0f30, 0x0f31, 0x0f32, 0x0f33, 0x0f34, 0x0f35, 0x0f36,
  40. 0x0f37, 0x0f38, 0x0f39, 0x0f3a, 0x0f3b, 0x0f3c, 0x0f3d, 0x0f3e,
  41. 0x0f3f, 0x0380, 0x0381, 0x0382, 0x0383, 0x0384, 0x0385, 0x0386,
  42. 0x0387, 0x0388, 0x0389, 0x038a, 0x038b, 0x038c, 0x038d, 0x038e,
  43. 0x038f, 0x0390, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396,
  44. 0x0397, 0x0398, 0x0399, 0x039a, 0x039b, 0x039c, 0x039d, 0x039e,
  45. 0x039f, 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5, 0x00c6,
  46. 0x00c7, 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce,
  47. 0x00cf, 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056,
  48. 0x0057, 0x0020, 0x0021, 0x0022, 0x0023, 0x000c, 0x000d, 0x0004,
  49. 0x0000, 0x0005, 0x000e, 0x000f, 0x0024, 0x0025, 0x0026, 0x0027,
  50. 0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
  51. 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
  52. 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df,
  53. 0x03a0, 0x03a1, 0x03a2, 0x03a3, 0x03a4, 0x03a5, 0x03a6, 0x03a7,
  54. 0x03a8, 0x03a9, 0x03aa, 0x03ab, 0x03ac, 0x03ad, 0x03ae, 0x03af,
  55. 0x03b0, 0x03b1, 0x03b2, 0x03b3, 0x03b4, 0x03b5, 0x03b6, 0x03b7,
  56. 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bc, 0x03bd, 0x03be, 0x03bf,
  57. 0x0f40, 0x0f41, 0x0f42, 0x0f43, 0x0f44, 0x0f45, 0x0f46, 0x0f47,
  58. 0x0f48, 0x0f49, 0x0f4a, 0x0f4b, 0x0f4c, 0x0f4d, 0x0f4e, 0x0f4f,
  59. 0x0f50, 0x0f51, 0x0f52, 0x0f53, 0x0f54, 0x0f55, 0x0f56, 0x0f57,
  60. 0x0f58, 0x0f59, 0x0f5a, 0x0f5b, 0x0f5c, 0x0f5d, 0x0f5e, 0x0f5f,
  61. 0x0f60, 0x0f61, 0x0f62, 0x0f63, 0x0f64, 0x0f65, 0x0f66, 0x0f67,
  62. 0x0f68, 0x0f69, 0x0f6a, 0x0f6b, 0x0f6c, 0x0f6d, 0x0f6e, 0x0f6f,
  63. 0x0f70, 0x0f71, 0x0f72, 0x0f73, 0x0f74, 0x0f75, 0x0f76, 0x0f77,
  64. 0x0f78, 0x0f79, 0x0f7a, 0x0f7b, 0x0f7c, 0x0f7d, 0x0f7e, 0x0f7f,
  65. };
  66. static const uint8_t rv_lum_bits[256] =
  67. {
  68. 14, 12, 12, 12, 12, 12, 12, 12,
  69. 12, 12, 12, 12, 12, 12, 12, 12,
  70. 12, 12, 12, 12, 12, 12, 12, 12,
  71. 12, 12, 12, 12, 12, 12, 12, 12,
  72. 12, 12, 12, 12, 12, 12, 12, 12,
  73. 12, 12, 12, 12, 12, 12, 12, 12,
  74. 12, 12, 12, 12, 12, 12, 12, 12,
  75. 12, 12, 12, 12, 12, 12, 12, 12,
  76. 12, 10, 10, 10, 10, 10, 10, 10,
  77. 10, 10, 10, 10, 10, 10, 10, 10,
  78. 10, 10, 10, 10, 10, 10, 10, 10,
  79. 10, 10, 10, 10, 10, 10, 10, 10,
  80. 10, 8, 8, 8, 8, 8, 8, 8,
  81. 8, 8, 8, 8, 8, 8, 8, 8,
  82. 8, 7, 7, 7, 7, 7, 7, 7,
  83. 7, 6, 6, 6, 6, 5, 5, 4,
  84. 2, 4, 5, 5, 6, 6, 6, 6,
  85. 7, 7, 7, 7, 7, 7, 7, 7,
  86. 8, 8, 8, 8, 8, 8, 8, 8,
  87. 8, 8, 8, 8, 8, 8, 8, 8,
  88. 10, 10, 10, 10, 10, 10, 10, 10,
  89. 10, 10, 10, 10, 10, 10, 10, 10,
  90. 10, 10, 10, 10, 10, 10, 10, 10,
  91. 10, 10, 10, 10, 10, 10, 10, 10,
  92. 12, 12, 12, 12, 12, 12, 12, 12,
  93. 12, 12, 12, 12, 12, 12, 12, 12,
  94. 12, 12, 12, 12, 12, 12, 12, 12,
  95. 12, 12, 12, 12, 12, 12, 12, 12,
  96. 12, 12, 12, 12, 12, 12, 12, 12,
  97. 12, 12, 12, 12, 12, 12, 12, 12,
  98. 12, 12, 12, 12, 12, 12, 12, 12,
  99. 12, 12, 12, 12, 12, 12, 12, 12,
  100. };
  101. static const uint16_t rv_chrom_code[256] =
  102. {
  103. 0xfe7f, 0x3f00, 0x3f01, 0x3f02, 0x3f03, 0x3f04, 0x3f05, 0x3f06,
  104. 0x3f07, 0x3f08, 0x3f09, 0x3f0a, 0x3f0b, 0x3f0c, 0x3f0d, 0x3f0e,
  105. 0x3f0f, 0x3f10, 0x3f11, 0x3f12, 0x3f13, 0x3f14, 0x3f15, 0x3f16,
  106. 0x3f17, 0x3f18, 0x3f19, 0x3f1a, 0x3f1b, 0x3f1c, 0x3f1d, 0x3f1e,
  107. 0x3f1f, 0x3f20, 0x3f21, 0x3f22, 0x3f23, 0x3f24, 0x3f25, 0x3f26,
  108. 0x3f27, 0x3f28, 0x3f29, 0x3f2a, 0x3f2b, 0x3f2c, 0x3f2d, 0x3f2e,
  109. 0x3f2f, 0x3f30, 0x3f31, 0x3f32, 0x3f33, 0x3f34, 0x3f35, 0x3f36,
  110. 0x3f37, 0x3f38, 0x3f39, 0x3f3a, 0x3f3b, 0x3f3c, 0x3f3d, 0x3f3e,
  111. 0x3f3f, 0x0f80, 0x0f81, 0x0f82, 0x0f83, 0x0f84, 0x0f85, 0x0f86,
  112. 0x0f87, 0x0f88, 0x0f89, 0x0f8a, 0x0f8b, 0x0f8c, 0x0f8d, 0x0f8e,
  113. 0x0f8f, 0x0f90, 0x0f91, 0x0f92, 0x0f93, 0x0f94, 0x0f95, 0x0f96,
  114. 0x0f97, 0x0f98, 0x0f99, 0x0f9a, 0x0f9b, 0x0f9c, 0x0f9d, 0x0f9e,
  115. 0x0f9f, 0x03c0, 0x03c1, 0x03c2, 0x03c3, 0x03c4, 0x03c5, 0x03c6,
  116. 0x03c7, 0x03c8, 0x03c9, 0x03ca, 0x03cb, 0x03cc, 0x03cd, 0x03ce,
  117. 0x03cf, 0x00e0, 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6,
  118. 0x00e7, 0x0030, 0x0031, 0x0032, 0x0033, 0x0008, 0x0009, 0x0002,
  119. 0x0000, 0x0003, 0x000a, 0x000b, 0x0034, 0x0035, 0x0036, 0x0037,
  120. 0x00e8, 0x00e9, 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef,
  121. 0x03d0, 0x03d1, 0x03d2, 0x03d3, 0x03d4, 0x03d5, 0x03d6, 0x03d7,
  122. 0x03d8, 0x03d9, 0x03da, 0x03db, 0x03dc, 0x03dd, 0x03de, 0x03df,
  123. 0x0fa0, 0x0fa1, 0x0fa2, 0x0fa3, 0x0fa4, 0x0fa5, 0x0fa6, 0x0fa7,
  124. 0x0fa8, 0x0fa9, 0x0faa, 0x0fab, 0x0fac, 0x0fad, 0x0fae, 0x0faf,
  125. 0x0fb0, 0x0fb1, 0x0fb2, 0x0fb3, 0x0fb4, 0x0fb5, 0x0fb6, 0x0fb7,
  126. 0x0fb8, 0x0fb9, 0x0fba, 0x0fbb, 0x0fbc, 0x0fbd, 0x0fbe, 0x0fbf,
  127. 0x3f40, 0x3f41, 0x3f42, 0x3f43, 0x3f44, 0x3f45, 0x3f46, 0x3f47,
  128. 0x3f48, 0x3f49, 0x3f4a, 0x3f4b, 0x3f4c, 0x3f4d, 0x3f4e, 0x3f4f,
  129. 0x3f50, 0x3f51, 0x3f52, 0x3f53, 0x3f54, 0x3f55, 0x3f56, 0x3f57,
  130. 0x3f58, 0x3f59, 0x3f5a, 0x3f5b, 0x3f5c, 0x3f5d, 0x3f5e, 0x3f5f,
  131. 0x3f60, 0x3f61, 0x3f62, 0x3f63, 0x3f64, 0x3f65, 0x3f66, 0x3f67,
  132. 0x3f68, 0x3f69, 0x3f6a, 0x3f6b, 0x3f6c, 0x3f6d, 0x3f6e, 0x3f6f,
  133. 0x3f70, 0x3f71, 0x3f72, 0x3f73, 0x3f74, 0x3f75, 0x3f76, 0x3f77,
  134. 0x3f78, 0x3f79, 0x3f7a, 0x3f7b, 0x3f7c, 0x3f7d, 0x3f7e, 0x3f7f,
  135. };
  136. static const uint8_t rv_chrom_bits[256] =
  137. {
  138. 16, 14, 14, 14, 14, 14, 14, 14,
  139. 14, 14, 14, 14, 14, 14, 14, 14,
  140. 14, 14, 14, 14, 14, 14, 14, 14,
  141. 14, 14, 14, 14, 14, 14, 14, 14,
  142. 14, 14, 14, 14, 14, 14, 14, 14,
  143. 14, 14, 14, 14, 14, 14, 14, 14,
  144. 14, 14, 14, 14, 14, 14, 14, 14,
  145. 14, 14, 14, 14, 14, 14, 14, 14,
  146. 14, 12, 12, 12, 12, 12, 12, 12,
  147. 12, 12, 12, 12, 12, 12, 12, 12,
  148. 12, 12, 12, 12, 12, 12, 12, 12,
  149. 12, 12, 12, 12, 12, 12, 12, 12,
  150. 12, 10, 10, 10, 10, 10, 10, 10,
  151. 10, 10, 10, 10, 10, 10, 10, 10,
  152. 10, 8, 8, 8, 8, 8, 8, 8,
  153. 8, 6, 6, 6, 6, 4, 4, 3,
  154. 2, 3, 4, 4, 6, 6, 6, 6,
  155. 8, 8, 8, 8, 8, 8, 8, 8,
  156. 10, 10, 10, 10, 10, 10, 10, 10,
  157. 10, 10, 10, 10, 10, 10, 10, 10,
  158. 12, 12, 12, 12, 12, 12, 12, 12,
  159. 12, 12, 12, 12, 12, 12, 12, 12,
  160. 12, 12, 12, 12, 12, 12, 12, 12,
  161. 12, 12, 12, 12, 12, 12, 12, 12,
  162. 14, 14, 14, 14, 14, 14, 14, 14,
  163. 14, 14, 14, 14, 14, 14, 14, 14,
  164. 14, 14, 14, 14, 14, 14, 14, 14,
  165. 14, 14, 14, 14, 14, 14, 14, 14,
  166. 14, 14, 14, 14, 14, 14, 14, 14,
  167. 14, 14, 14, 14, 14, 14, 14, 14,
  168. 14, 14, 14, 14, 14, 14, 14, 14,
  169. 14, 14, 14, 14, 14, 14, 14, 14,
  170. };
  171. static VLC rv_dc_lum, rv_dc_chrom;
  172. int rv_decode_dc(MpegEncContext *s, int n)
  173. {
  174. int code;
  175. if (n < 4) {
  176. code = get_vlc2(&s->gb, rv_dc_lum.table, DC_VLC_BITS, 2);
  177. if (code < 0) {
  178. /* XXX: I don't understand why they use LONGER codes than
  179. necessary. The following code would be completely useless
  180. if they had thought about it !!! */
  181. code = get_bits(&s->gb, 7);
  182. if (code == 0x7c) {
  183. code = (int8_t)(get_bits(&s->gb, 7) + 1);
  184. } else if (code == 0x7d) {
  185. code = -128 + get_bits(&s->gb, 7);
  186. } else if (code == 0x7e) {
  187. if (get_bits1(&s->gb) == 0)
  188. code = (int8_t)(get_bits(&s->gb, 8) + 1);
  189. else
  190. code = (int8_t)(get_bits(&s->gb, 8));
  191. } else if (code == 0x7f) {
  192. skip_bits(&s->gb, 11);
  193. code = 1;
  194. }
  195. } else {
  196. code -= 128;
  197. }
  198. } else {
  199. code = get_vlc2(&s->gb, rv_dc_chrom.table, DC_VLC_BITS, 2);
  200. /* same remark */
  201. if (code < 0) {
  202. code = get_bits(&s->gb, 9);
  203. if (code == 0x1fc) {
  204. code = (int8_t)(get_bits(&s->gb, 7) + 1);
  205. } else if (code == 0x1fd) {
  206. code = -128 + get_bits(&s->gb, 7);
  207. } else if (code == 0x1fe) {
  208. skip_bits(&s->gb, 9);
  209. code = 1;
  210. } else {
  211. av_log(s->avctx, AV_LOG_ERROR, "chroma dc error\n");
  212. return 0xffff;
  213. }
  214. } else {
  215. code -= 128;
  216. }
  217. }
  218. return -code;
  219. }
  220. #if CONFIG_RV10_ENCODER || CONFIG_RV20_ENCODER
  221. /* write RV 1.0 compatible frame header */
  222. void rv10_encode_picture_header(MpegEncContext *s, int picture_number)
  223. {
  224. int full_frame= 0;
  225. align_put_bits(&s->pb);
  226. put_bits(&s->pb, 1, 1); /* marker */
  227. put_bits(&s->pb, 1, (s->pict_type == FF_P_TYPE));
  228. put_bits(&s->pb, 1, 0); /* not PB frame */
  229. put_bits(&s->pb, 5, s->qscale);
  230. if (s->pict_type == FF_I_TYPE) {
  231. /* specific MPEG like DC coding not used */
  232. }
  233. /* if multiple packets per frame are sent, the position at which
  234. to display the macroblocks is coded here */
  235. if(!full_frame){
  236. put_bits(&s->pb, 6, 0); /* mb_x */
  237. put_bits(&s->pb, 6, 0); /* mb_y */
  238. put_bits(&s->pb, 12, s->mb_width * s->mb_height);
  239. }
  240. put_bits(&s->pb, 3, 0); /* ignored */
  241. }
  242. void rv20_encode_picture_header(MpegEncContext *s, int picture_number){
  243. put_bits(&s->pb, 2, s->pict_type); //I 0 vs. 1 ?
  244. put_bits(&s->pb, 1, 0); /* unknown bit */
  245. put_bits(&s->pb, 5, s->qscale);
  246. put_sbits(&s->pb, 8, picture_number); //FIXME wrong, but correct is not known
  247. s->mb_x= s->mb_y= 0;
  248. ff_h263_encode_mba(s);
  249. put_bits(&s->pb, 1, s->no_rounding);
  250. assert(s->f_code == 1);
  251. assert(s->unrestricted_mv == 1);
  252. // assert(s->h263_aic== (s->pict_type == FF_I_TYPE));
  253. assert(s->alt_inter_vlc == 0);
  254. assert(s->umvplus == 0);
  255. assert(s->modified_quant==1);
  256. assert(s->loop_filter==1);
  257. s->h263_aic= s->pict_type == FF_I_TYPE;
  258. if(s->h263_aic){
  259. s->y_dc_scale_table=
  260. s->c_dc_scale_table= ff_aic_dc_scale_table;
  261. }else{
  262. s->y_dc_scale_table=
  263. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  264. }
  265. }
  266. #endif /* CONFIG_RV10_ENCODER || CONFIG_RV20_ENCODER */
  267. /* read RV 1.0 compatible frame header */
  268. static int rv10_decode_picture_header(MpegEncContext *s)
  269. {
  270. int mb_count, pb_frame, marker, unk, mb_xy;
  271. //printf("ff:%d\n", full_frame);
  272. marker = get_bits1(&s->gb);
  273. if (get_bits1(&s->gb))
  274. s->pict_type = FF_P_TYPE;
  275. else
  276. s->pict_type = FF_I_TYPE;
  277. //printf("h:%X ver:%d\n",h,s->rv10_version);
  278. if(!marker) av_log(s->avctx, AV_LOG_ERROR, "marker missing\n");
  279. pb_frame = get_bits1(&s->gb);
  280. #ifdef DEBUG
  281. av_log(s->avctx, AV_LOG_DEBUG, "pict_type=%d pb_frame=%d\n", s->pict_type, pb_frame);
  282. #endif
  283. if (pb_frame){
  284. av_log(s->avctx, AV_LOG_ERROR, "pb frame not supported\n");
  285. return -1;
  286. }
  287. s->qscale = get_bits(&s->gb, 5);
  288. if(s->qscale==0){
  289. av_log(s->avctx, AV_LOG_ERROR, "error, qscale:0\n");
  290. return -1;
  291. }
  292. if (s->pict_type == FF_I_TYPE) {
  293. if (s->rv10_version == 3) {
  294. /* specific MPEG like DC coding not used */
  295. s->last_dc[0] = get_bits(&s->gb, 8);
  296. s->last_dc[1] = get_bits(&s->gb, 8);
  297. s->last_dc[2] = get_bits(&s->gb, 8);
  298. #ifdef DEBUG
  299. av_log(s->avctx, AV_LOG_DEBUG, "DC:%d %d %d\n",
  300. s->last_dc[0],
  301. s->last_dc[1],
  302. s->last_dc[2]);
  303. #endif
  304. }
  305. }
  306. /* if multiple packets per frame are sent, the position at which
  307. to display the macroblocks is coded here */
  308. mb_xy= s->mb_x + s->mb_y*s->mb_width;
  309. if(show_bits(&s->gb, 12)==0 || (mb_xy && mb_xy < s->mb_num)){
  310. s->mb_x = get_bits(&s->gb, 6); /* mb_x */
  311. s->mb_y = get_bits(&s->gb, 6); /* mb_y */
  312. mb_count = get_bits(&s->gb, 12);
  313. } else {
  314. s->mb_x = 0;
  315. s->mb_y = 0;
  316. mb_count = s->mb_width * s->mb_height;
  317. }
  318. unk= get_bits(&s->gb, 3); /* ignored */
  319. //printf("%d\n", unk);
  320. s->f_code = 1;
  321. s->unrestricted_mv = 1;
  322. return mb_count;
  323. }
  324. static int rv20_decode_picture_header(MpegEncContext *s)
  325. {
  326. int seq, mb_pos, i;
  327. #if 0
  328. GetBitContext gb= s->gb;
  329. for(i=0; i<64; i++){
  330. av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&gb));
  331. if(i%4==3) av_log(s->avctx, AV_LOG_DEBUG, " ");
  332. }
  333. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  334. #endif
  335. #if 0
  336. av_log(s->avctx, AV_LOG_DEBUG, "%3dx%03d/%02Xx%02X ", s->width, s->height, s->width/4, s->height/4);
  337. for(i=0; i<s->avctx->extradata_size; i++){
  338. av_log(s->avctx, AV_LOG_DEBUG, "%02X ", ((uint8_t*)s->avctx->extradata)[i]);
  339. if(i%4==3) av_log(s->avctx, AV_LOG_DEBUG, " ");
  340. }
  341. av_log(s->avctx, AV_LOG_DEBUG, "\n");
  342. #endif
  343. if(s->avctx->sub_id == 0x30202002 || s->avctx->sub_id == 0x30203002){
  344. if (get_bits(&s->gb, 3)){
  345. av_log(s->avctx, AV_LOG_ERROR, "unknown triplet set\n");
  346. return -1;
  347. }
  348. }
  349. i= get_bits(&s->gb, 2);
  350. switch(i){
  351. case 0: s->pict_type= FF_I_TYPE; break;
  352. case 1: s->pict_type= FF_I_TYPE; break; //hmm ...
  353. case 2: s->pict_type= FF_P_TYPE; break;
  354. case 3: s->pict_type= FF_B_TYPE; break;
  355. default:
  356. av_log(s->avctx, AV_LOG_ERROR, "unknown frame type\n");
  357. return -1;
  358. }
  359. if(s->last_picture_ptr==NULL && s->pict_type==FF_B_TYPE){
  360. av_log(s->avctx, AV_LOG_ERROR, "early B pix\n");
  361. return -1;
  362. }
  363. if (get_bits1(&s->gb)){
  364. av_log(s->avctx, AV_LOG_ERROR, "unknown bit set\n");
  365. return -1;
  366. }
  367. s->qscale = get_bits(&s->gb, 5);
  368. if(s->qscale==0){
  369. av_log(s->avctx, AV_LOG_ERROR, "error, qscale:0\n");
  370. return -1;
  371. }
  372. if(s->avctx->sub_id == 0x30203002){
  373. if (get_bits1(&s->gb)){
  374. av_log(s->avctx, AV_LOG_ERROR, "unknown bit2 set\n");
  375. return -1;
  376. }
  377. }
  378. if(s->avctx->has_b_frames){
  379. int f, new_w, new_h;
  380. int v= s->avctx->extradata_size >= 4 ? 7&((uint8_t*)s->avctx->extradata)[1] : 0;
  381. if (get_bits1(&s->gb)){
  382. av_log(s->avctx, AV_LOG_ERROR, "unknown bit3 set\n");
  383. // return -1;
  384. }
  385. seq= get_bits(&s->gb, 13)<<2;
  386. f= get_bits(&s->gb, av_log2(v)+1);
  387. if(f){
  388. new_w= 4*((uint8_t*)s->avctx->extradata)[6+2*f];
  389. new_h= 4*((uint8_t*)s->avctx->extradata)[7+2*f];
  390. }else{
  391. new_w= s->width; //FIXME wrong we of course must save the original in the context
  392. new_h= s->height;
  393. }
  394. if(new_w != s->width || new_h != s->height){
  395. av_log(s->avctx, AV_LOG_DEBUG, "attempting to change resolution to %dx%d\n", new_w, new_h);
  396. if (avcodec_check_dimensions(s->avctx, new_h, new_w) < 0)
  397. return -1;
  398. MPV_common_end(s);
  399. s->width = s->avctx->width = new_w;
  400. s->height = s->avctx->height= new_h;
  401. if (MPV_common_init(s) < 0)
  402. return -1;
  403. }
  404. if(s->avctx->debug & FF_DEBUG_PICT_INFO){
  405. av_log(s->avctx, AV_LOG_DEBUG, "F %d/%d\n", f, v);
  406. }
  407. }else{
  408. seq= get_bits(&s->gb, 8)*128;
  409. }
  410. // if(s->avctx->sub_id <= 0x20201002){ //0x20201002 definitely needs this
  411. mb_pos= ff_h263_decode_mba(s);
  412. /* }else{
  413. mb_pos= get_bits(&s->gb, av_log2(s->mb_num-1)+1);
  414. s->mb_x= mb_pos % s->mb_width;
  415. s->mb_y= mb_pos / s->mb_width;
  416. }*/
  417. //av_log(s->avctx, AV_LOG_DEBUG, "%d\n", seq);
  418. seq |= s->time &~0x7FFF;
  419. if(seq - s->time > 0x4000) seq -= 0x8000;
  420. if(seq - s->time < -0x4000) seq += 0x8000;
  421. if(seq != s->time){
  422. if(s->pict_type!=FF_B_TYPE){
  423. s->time= seq;
  424. s->pp_time= s->time - s->last_non_b_time;
  425. s->last_non_b_time= s->time;
  426. }else{
  427. s->time= seq;
  428. s->pb_time= s->pp_time - (s->last_non_b_time - s->time);
  429. if(s->pp_time <=s->pb_time || s->pp_time <= s->pp_time - s->pb_time || s->pp_time<=0){
  430. av_log(s->avctx, AV_LOG_DEBUG, "messed up order, possible from seeking? skipping current b frame\n");
  431. return FRAME_SKIPPED;
  432. }
  433. ff_mpeg4_init_direct_mv(s);
  434. }
  435. }
  436. // printf("%d %d %d %d %d\n", seq, (int)s->time, (int)s->last_non_b_time, s->pp_time, s->pb_time);
  437. /*for(i=0; i<32; i++){
  438. av_log(s->avctx, AV_LOG_DEBUG, "%d", get_bits1(&s->gb));
  439. }
  440. av_log(s->avctx, AV_LOG_DEBUG, "\n");*/
  441. s->no_rounding= get_bits1(&s->gb);
  442. s->f_code = 1;
  443. s->unrestricted_mv = 1;
  444. s->h263_aic= s->pict_type == FF_I_TYPE;
  445. // s->alt_inter_vlc=1;
  446. // s->obmc=1;
  447. // s->umvplus=1;
  448. s->modified_quant=1;
  449. if(!s->avctx->lowres)
  450. s->loop_filter=1;
  451. if(s->avctx->debug & FF_DEBUG_PICT_INFO){
  452. av_log(s->avctx, AV_LOG_INFO, "num:%5d x:%2d y:%2d type:%d qscale:%2d rnd:%d\n",
  453. seq, s->mb_x, s->mb_y, s->pict_type, s->qscale, s->no_rounding);
  454. }
  455. assert(s->pict_type != FF_B_TYPE || !s->low_delay);
  456. return s->mb_width*s->mb_height - mb_pos;
  457. }
  458. static av_cold int rv10_decode_init(AVCodecContext *avctx)
  459. {
  460. MpegEncContext *s = avctx->priv_data;
  461. static int done=0;
  462. if (avctx->extradata_size < 8) {
  463. av_log(avctx, AV_LOG_ERROR, "Extradata is too small.\n");
  464. return -1;
  465. }
  466. MPV_decode_defaults(s);
  467. s->avctx= avctx;
  468. s->out_format = FMT_H263;
  469. s->codec_id= avctx->codec_id;
  470. s->width = avctx->coded_width;
  471. s->height = avctx->coded_height;
  472. s->h263_long_vectors= ((uint8_t*)avctx->extradata)[3] & 1;
  473. avctx->sub_id= AV_RB32((uint8_t*)avctx->extradata + 4);
  474. if (avctx->sub_id == 0x10000000) {
  475. s->rv10_version= 0;
  476. s->low_delay=1;
  477. } else if (avctx->sub_id == 0x10001000) {
  478. s->rv10_version= 3;
  479. s->low_delay=1;
  480. } else if (avctx->sub_id == 0x10002000) {
  481. s->rv10_version= 3;
  482. s->low_delay=1;
  483. s->obmc=1;
  484. } else if (avctx->sub_id == 0x10003000) {
  485. s->rv10_version= 3;
  486. s->low_delay=1;
  487. } else if (avctx->sub_id == 0x10003001) {
  488. s->rv10_version= 3;
  489. s->low_delay=1;
  490. } else if ( avctx->sub_id == 0x20001000
  491. || (avctx->sub_id >= 0x20100000 && avctx->sub_id < 0x201a0000)) {
  492. s->low_delay=1;
  493. } else if ( avctx->sub_id == 0x30202002
  494. || avctx->sub_id == 0x30203002
  495. || (avctx->sub_id >= 0x20200002 && avctx->sub_id < 0x20300000)) {
  496. s->low_delay=0;
  497. s->avctx->has_b_frames=1;
  498. } else
  499. av_log(s->avctx, AV_LOG_ERROR, "unknown header %X\n", avctx->sub_id);
  500. if(avctx->debug & FF_DEBUG_PICT_INFO){
  501. av_log(avctx, AV_LOG_DEBUG, "ver:%X ver0:%X\n", avctx->sub_id, avctx->extradata_size >= 4 ? ((uint32_t*)avctx->extradata)[0] : -1);
  502. }
  503. avctx->pix_fmt = PIX_FMT_YUV420P;
  504. if (MPV_common_init(s) < 0)
  505. return -1;
  506. h263_decode_init_vlc(s);
  507. /* init rv vlc */
  508. if (!done) {
  509. INIT_VLC_STATIC(&rv_dc_lum, DC_VLC_BITS, 256,
  510. rv_lum_bits, 1, 1,
  511. rv_lum_code, 2, 2, 16384);
  512. INIT_VLC_STATIC(&rv_dc_chrom, DC_VLC_BITS, 256,
  513. rv_chrom_bits, 1, 1,
  514. rv_chrom_code, 2, 2, 16388);
  515. done = 1;
  516. }
  517. return 0;
  518. }
  519. static av_cold int rv10_decode_end(AVCodecContext *avctx)
  520. {
  521. MpegEncContext *s = avctx->priv_data;
  522. MPV_common_end(s);
  523. return 0;
  524. }
  525. static int rv10_decode_packet(AVCodecContext *avctx,
  526. const uint8_t *buf, int buf_size)
  527. {
  528. MpegEncContext *s = avctx->priv_data;
  529. int mb_count, mb_pos, left, start_mb_x;
  530. init_get_bits(&s->gb, buf, buf_size*8);
  531. if(s->codec_id ==CODEC_ID_RV10)
  532. mb_count = rv10_decode_picture_header(s);
  533. else
  534. mb_count = rv20_decode_picture_header(s);
  535. if (mb_count < 0) {
  536. av_log(s->avctx, AV_LOG_ERROR, "HEADER ERROR\n");
  537. return -1;
  538. }
  539. if (s->mb_x >= s->mb_width ||
  540. s->mb_y >= s->mb_height) {
  541. av_log(s->avctx, AV_LOG_ERROR, "POS ERROR %d %d\n", s->mb_x, s->mb_y);
  542. return -1;
  543. }
  544. mb_pos = s->mb_y * s->mb_width + s->mb_x;
  545. left = s->mb_width * s->mb_height - mb_pos;
  546. if (mb_count > left) {
  547. av_log(s->avctx, AV_LOG_ERROR, "COUNT ERROR\n");
  548. return -1;
  549. }
  550. //if(s->pict_type == FF_P_TYPE) return 0;
  551. if ((s->mb_x == 0 && s->mb_y == 0) || s->current_picture_ptr==NULL) {
  552. if(s->current_picture_ptr){ //FIXME write parser so we always have complete frames?
  553. ff_er_frame_end(s);
  554. MPV_frame_end(s);
  555. s->mb_x= s->mb_y = s->resync_mb_x = s->resync_mb_y= 0;
  556. }
  557. if(MPV_frame_start(s, avctx) < 0)
  558. return -1;
  559. ff_er_frame_start(s);
  560. }
  561. #ifdef DEBUG
  562. av_log(avctx, AV_LOG_DEBUG, "qscale=%d\n", s->qscale);
  563. #endif
  564. /* default quantization values */
  565. if(s->codec_id== CODEC_ID_RV10){
  566. if(s->mb_y==0) s->first_slice_line=1;
  567. }else{
  568. s->first_slice_line=1;
  569. s->resync_mb_x= s->mb_x;
  570. }
  571. start_mb_x= s->mb_x;
  572. s->resync_mb_y= s->mb_y;
  573. if(s->h263_aic){
  574. s->y_dc_scale_table=
  575. s->c_dc_scale_table= ff_aic_dc_scale_table;
  576. }else{
  577. s->y_dc_scale_table=
  578. s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
  579. }
  580. if(s->modified_quant)
  581. s->chroma_qscale_table= ff_h263_chroma_qscale_table;
  582. ff_set_qscale(s, s->qscale);
  583. s->rv10_first_dc_coded[0] = 0;
  584. s->rv10_first_dc_coded[1] = 0;
  585. s->rv10_first_dc_coded[2] = 0;
  586. //printf("%d %X %X\n", s->pict_type, s->current_picture.motion_val[0], s->current_picture.motion_val[1]);
  587. s->block_wrap[0]=
  588. s->block_wrap[1]=
  589. s->block_wrap[2]=
  590. s->block_wrap[3]= s->b8_stride;
  591. s->block_wrap[4]=
  592. s->block_wrap[5]= s->mb_stride;
  593. ff_init_block_index(s);
  594. /* decode each macroblock */
  595. for(s->mb_num_left= mb_count; s->mb_num_left>0; s->mb_num_left--) {
  596. int ret;
  597. ff_update_block_index(s);
  598. #ifdef DEBUG
  599. av_log(avctx, AV_LOG_DEBUG, "**mb x=%d y=%d\n", s->mb_x, s->mb_y);
  600. #endif
  601. s->mv_dir = MV_DIR_FORWARD;
  602. s->mv_type = MV_TYPE_16X16;
  603. ret=ff_h263_decode_mb(s, s->block);
  604. if (ret == SLICE_ERROR || s->gb.size_in_bits < get_bits_count(&s->gb)) {
  605. av_log(s->avctx, AV_LOG_ERROR, "ERROR at MB %d %d\n", s->mb_x, s->mb_y);
  606. return -1;
  607. }
  608. if(s->pict_type != FF_B_TYPE)
  609. ff_h263_update_motion_val(s);
  610. MPV_decode_mb(s, s->block);
  611. if(s->loop_filter)
  612. ff_h263_loop_filter(s);
  613. if (++s->mb_x == s->mb_width) {
  614. s->mb_x = 0;
  615. s->mb_y++;
  616. ff_init_block_index(s);
  617. }
  618. if(s->mb_x == s->resync_mb_x)
  619. s->first_slice_line=0;
  620. if(ret == SLICE_END) break;
  621. }
  622. ff_er_add_slice(s, start_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
  623. return buf_size;
  624. }
  625. static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n)
  626. {
  627. if(avctx->slice_count) return avctx->slice_offset[n];
  628. else return AV_RL32(buf + n*8);
  629. }
  630. static int rv10_decode_frame(AVCodecContext *avctx,
  631. void *data, int *data_size,
  632. AVPacket *avpkt)
  633. {
  634. const uint8_t *buf = avpkt->data;
  635. int buf_size = avpkt->size;
  636. MpegEncContext *s = avctx->priv_data;
  637. int i;
  638. AVFrame *pict = data;
  639. int slice_count;
  640. const uint8_t *slices_hdr = NULL;
  641. #ifdef DEBUG
  642. av_log(avctx, AV_LOG_DEBUG, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
  643. #endif
  644. /* no supplementary picture */
  645. if (buf_size == 0) {
  646. return 0;
  647. }
  648. if(!avctx->slice_count){
  649. slice_count = (*buf++) + 1;
  650. slices_hdr = buf + 4;
  651. buf += 8 * slice_count;
  652. }else
  653. slice_count = avctx->slice_count;
  654. for(i=0; i<slice_count; i++){
  655. int offset= get_slice_offset(avctx, slices_hdr, i);
  656. int size;
  657. if(i+1 == slice_count)
  658. size= buf_size - offset;
  659. else
  660. size= get_slice_offset(avctx, slices_hdr, i+1) - offset;
  661. rv10_decode_packet(avctx, buf+offset, size);
  662. }
  663. if(s->current_picture_ptr != NULL && s->mb_y>=s->mb_height){
  664. ff_er_frame_end(s);
  665. MPV_frame_end(s);
  666. if (s->pict_type == FF_B_TYPE || s->low_delay) {
  667. *pict= *(AVFrame*)s->current_picture_ptr;
  668. } else if (s->last_picture_ptr != NULL) {
  669. *pict= *(AVFrame*)s->last_picture_ptr;
  670. }
  671. if(s->last_picture_ptr || s->low_delay){
  672. *data_size = sizeof(AVFrame);
  673. ff_print_debug_info(s, pict);
  674. }
  675. s->current_picture_ptr= NULL; //so we can detect if frame_end wasnt called (find some nicer solution...)
  676. }
  677. return buf_size;
  678. }
  679. AVCodec rv10_decoder = {
  680. "rv10",
  681. CODEC_TYPE_VIDEO,
  682. CODEC_ID_RV10,
  683. sizeof(MpegEncContext),
  684. rv10_decode_init,
  685. NULL,
  686. rv10_decode_end,
  687. rv10_decode_frame,
  688. CODEC_CAP_DR1,
  689. .long_name = NULL_IF_CONFIG_SMALL("RealVideo 1.0"),
  690. .pix_fmts= ff_pixfmt_list_420,
  691. };
  692. AVCodec rv20_decoder = {
  693. "rv20",
  694. CODEC_TYPE_VIDEO,
  695. CODEC_ID_RV20,
  696. sizeof(MpegEncContext),
  697. rv10_decode_init,
  698. NULL,
  699. rv10_decode_end,
  700. rv10_decode_frame,
  701. CODEC_CAP_DR1 | CODEC_CAP_DELAY,
  702. .flush= ff_mpeg_flush,
  703. .long_name = NULL_IF_CONFIG_SMALL("RealVideo 2.0"),
  704. .pix_fmts= ff_pixfmt_list_420,
  705. };