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.

720 lines
22KB

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