h264enc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * H.264 encoder
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/common.h"
  21. #include "bitstream.h"
  22. #include "mpegvideo.h"
  23. #include "h264data.h"
  24. /**
  25. * Write out the provided data into a NAL unit.
  26. * @param nal_ref_idc NAL reference IDC
  27. * @param nal_unit_type NAL unit payload type
  28. * @param dest the target buffer, dst+1 == src is allowed as a special case
  29. * @param destsize the length of the dst array
  30. * @param b2 the data which should be escaped
  31. * @returns pointer to current position in the output buffer or NULL if an error occurred
  32. */
  33. static uint8_t *h264_write_nal_unit(int nal_ref_idc, int nal_unit_type, uint8_t *dest, int *destsize,
  34. PutBitContext *b2)
  35. {
  36. PutBitContext b;
  37. int i, destpos, rbsplen, escape_count;
  38. uint8_t *rbsp;
  39. if (nal_unit_type != NAL_END_STREAM)
  40. put_bits(b2,1,1); // rbsp_stop_bit
  41. // Align b2 on a byte boundary
  42. align_put_bits(b2);
  43. rbsplen = put_bits_count(b2)/8;
  44. flush_put_bits(b2);
  45. rbsp = b2->buf;
  46. init_put_bits(&b,dest,*destsize);
  47. put_bits(&b,16,0);
  48. put_bits(&b,16,0x01);
  49. put_bits(&b,1,0); // forbidden zero bit
  50. put_bits(&b,2,nal_ref_idc); // nal_ref_idc
  51. put_bits(&b,5,nal_unit_type); // nal_unit_type
  52. flush_put_bits(&b);
  53. destpos = 5;
  54. escape_count= 0;
  55. for (i=0; i<rbsplen; i+=2)
  56. {
  57. if (rbsp[i]) continue;
  58. if (i>0 && rbsp[i-1]==0)
  59. i--;
  60. if (i+2<rbsplen && rbsp[i+1]==0 && rbsp[i+2]<=3)
  61. {
  62. escape_count++;
  63. i+=2;
  64. }
  65. }
  66. if(escape_count==0)
  67. {
  68. if(dest+destpos != rbsp)
  69. {
  70. memcpy(dest+destpos, rbsp, rbsplen);
  71. *destsize -= (rbsplen+destpos);
  72. }
  73. return dest+rbsplen+destpos;
  74. }
  75. if(rbsplen + escape_count + 1> *destsize)
  76. {
  77. av_log(NULL, AV_LOG_ERROR, "Destination buffer too small!\n");
  78. return NULL;
  79. }
  80. // this should be damn rare (hopefully)
  81. for (i = 0 ; i < rbsplen ; i++)
  82. {
  83. if (i + 2 < rbsplen && (rbsp[i] == 0 && rbsp[i+1] == 0 && rbsp[i+2] < 4))
  84. {
  85. dest[destpos++] = rbsp[i++];
  86. dest[destpos++] = rbsp[i];
  87. dest[destpos++] = 0x03; // emulation prevention byte
  88. }
  89. else
  90. dest[destpos++] = rbsp[i];
  91. }
  92. *destsize -= destpos;
  93. return dest+destpos;
  94. }
  95. static const uint8_t pict_type_to_golomb[7] = {-1, 2, 0, 1, -1, 4, 3};
  96. static const uint8_t intra4x4_cbp_to_golomb[48] = {
  97. 3, 29, 30, 17, 31, 18, 37, 8, 32, 38, 19, 9, 20, 10, 11, 2,
  98. 16, 33, 34, 21, 35, 22, 39, 4, 36, 40, 23, 5, 24, 6, 7, 1,
  99. 41, 42, 43, 25, 44, 26, 46, 12, 45, 47, 27, 13, 28, 14, 15, 0
  100. };
  101. static const uint8_t inter_cbp_to_golomb[48] = {
  102. 0, 2, 3, 7, 4, 8, 17, 13, 5, 18, 9, 14, 10, 15, 16, 11,
  103. 1, 32, 33, 36, 34, 37, 44, 40, 35, 45, 38, 41, 39, 42, 43, 19,
  104. 6, 24, 25, 20, 26, 21, 46, 28, 27, 47, 22, 29, 23, 30, 31, 12
  105. };
  106. #define QUANT_SHIFT 22
  107. static const int quant_coeff[52][16] = {
  108. { 419430, 258111, 419430, 258111, 258111, 167772, 258111, 167772, 419430, 258111, 419430, 258111, 258111, 167772, 258111, 167772,},
  109. { 381300, 239675, 381300, 239675, 239675, 149131, 239675, 149131, 381300, 239675, 381300, 239675, 239675, 149131, 239675, 149131,},
  110. { 322639, 209715, 322639, 209715, 209715, 134218, 209715, 134218, 322639, 209715, 322639, 209715, 209715, 134218, 209715, 134218,},
  111. { 299593, 186414, 299593, 186414, 186414, 116711, 186414, 116711, 299593, 186414, 299593, 186414, 186414, 116711, 186414, 116711,},
  112. { 262144, 167772, 262144, 167772, 167772, 107374, 167772, 107374, 262144, 167772, 262144, 167772, 167772, 107374, 167772, 107374,},
  113. { 233017, 145889, 233017, 145889, 145889, 92564, 145889, 92564, 233017, 145889, 233017, 145889, 145889, 92564, 145889, 92564,},
  114. { 209715, 129056, 209715, 129056, 129056, 83886, 129056, 83886, 209715, 129056, 209715, 129056, 129056, 83886, 129056, 83886,},
  115. { 190650, 119837, 190650, 119837, 119837, 74565, 119837, 74565, 190650, 119837, 190650, 119837, 119837, 74565, 119837, 74565,},
  116. { 161319, 104858, 161319, 104858, 104858, 67109, 104858, 67109, 161319, 104858, 161319, 104858, 104858, 67109, 104858, 67109,},
  117. { 149797, 93207, 149797, 93207, 93207, 58356, 93207, 58356, 149797, 93207, 149797, 93207, 93207, 58356, 93207, 58356,},
  118. { 131072, 83886, 131072, 83886, 83886, 53687, 83886, 53687, 131072, 83886, 131072, 83886, 83886, 53687, 83886, 53687,},
  119. { 116508, 72944, 116508, 72944, 72944, 46282, 72944, 46282, 116508, 72944, 116508, 72944, 72944, 46282, 72944, 46282,},
  120. { 104858, 64528, 104858, 64528, 64528, 41943, 64528, 41943, 104858, 64528, 104858, 64528, 64528, 41943, 64528, 41943,},
  121. { 95325, 59919, 95325, 59919, 59919, 37283, 59919, 37283, 95325, 59919, 95325, 59919, 59919, 37283, 59919, 37283,},
  122. { 80660, 52429, 80660, 52429, 52429, 33554, 52429, 33554, 80660, 52429, 80660, 52429, 52429, 33554, 52429, 33554,},
  123. { 74898, 46603, 74898, 46603, 46603, 29178, 46603, 29178, 74898, 46603, 74898, 46603, 46603, 29178, 46603, 29178,},
  124. { 65536, 41943, 65536, 41943, 41943, 26844, 41943, 26844, 65536, 41943, 65536, 41943, 41943, 26844, 41943, 26844,},
  125. { 58254, 36472, 58254, 36472, 36472, 23141, 36472, 23141, 58254, 36472, 58254, 36472, 36472, 23141, 36472, 23141,},
  126. { 52429, 32264, 52429, 32264, 32264, 20972, 32264, 20972, 52429, 32264, 52429, 32264, 32264, 20972, 32264, 20972,},
  127. { 47663, 29959, 47663, 29959, 29959, 18641, 29959, 18641, 47663, 29959, 47663, 29959, 29959, 18641, 29959, 18641,},
  128. { 40330, 26214, 40330, 26214, 26214, 16777, 26214, 16777, 40330, 26214, 40330, 26214, 26214, 16777, 26214, 16777,},
  129. { 37449, 23302, 37449, 23302, 23302, 14589, 23302, 14589, 37449, 23302, 37449, 23302, 23302, 14589, 23302, 14589,},
  130. { 32768, 20972, 32768, 20972, 20972, 13422, 20972, 13422, 32768, 20972, 32768, 20972, 20972, 13422, 20972, 13422,},
  131. { 29127, 18236, 29127, 18236, 18236, 11570, 18236, 11570, 29127, 18236, 29127, 18236, 18236, 11570, 18236, 11570,},
  132. { 26214, 16132, 26214, 16132, 16132, 10486, 16132, 10486, 26214, 16132, 26214, 16132, 16132, 10486, 16132, 10486,},
  133. { 23831, 14980, 23831, 14980, 14980, 9321, 14980, 9321, 23831, 14980, 23831, 14980, 14980, 9321, 14980, 9321,},
  134. { 20165, 13107, 20165, 13107, 13107, 8389, 13107, 8389, 20165, 13107, 20165, 13107, 13107, 8389, 13107, 8389,},
  135. { 18725, 11651, 18725, 11651, 11651, 7294, 11651, 7294, 18725, 11651, 18725, 11651, 11651, 7294, 11651, 7294,},
  136. { 16384, 10486, 16384, 10486, 10486, 6711, 10486, 6711, 16384, 10486, 16384, 10486, 10486, 6711, 10486, 6711,},
  137. { 14564, 9118, 14564, 9118, 9118, 5785, 9118, 5785, 14564, 9118, 14564, 9118, 9118, 5785, 9118, 5785,},
  138. { 13107, 8066, 13107, 8066, 8066, 5243, 8066, 5243, 13107, 8066, 13107, 8066, 8066, 5243, 8066, 5243,},
  139. { 11916, 7490, 11916, 7490, 7490, 4660, 7490, 4660, 11916, 7490, 11916, 7490, 7490, 4660, 7490, 4660,},
  140. { 10082, 6554, 10082, 6554, 6554, 4194, 6554, 4194, 10082, 6554, 10082, 6554, 6554, 4194, 6554, 4194,},
  141. { 9362, 5825, 9362, 5825, 5825, 3647, 5825, 3647, 9362, 5825, 9362, 5825, 5825, 3647, 5825, 3647,},
  142. { 8192, 5243, 8192, 5243, 5243, 3355, 5243, 3355, 8192, 5243, 8192, 5243, 5243, 3355, 5243, 3355,},
  143. { 7282, 4559, 7282, 4559, 4559, 2893, 4559, 2893, 7282, 4559, 7282, 4559, 4559, 2893, 4559, 2893,},
  144. { 6554, 4033, 6554, 4033, 4033, 2621, 4033, 2621, 6554, 4033, 6554, 4033, 4033, 2621, 4033, 2621,},
  145. { 5958, 3745, 5958, 3745, 3745, 2330, 3745, 2330, 5958, 3745, 5958, 3745, 3745, 2330, 3745, 2330,},
  146. { 5041, 3277, 5041, 3277, 3277, 2097, 3277, 2097, 5041, 3277, 5041, 3277, 3277, 2097, 3277, 2097,},
  147. { 4681, 2913, 4681, 2913, 2913, 1824, 2913, 1824, 4681, 2913, 4681, 2913, 2913, 1824, 2913, 1824,},
  148. { 4096, 2621, 4096, 2621, 2621, 1678, 2621, 1678, 4096, 2621, 4096, 2621, 2621, 1678, 2621, 1678,},
  149. { 3641, 2280, 3641, 2280, 2280, 1446, 2280, 1446, 3641, 2280, 3641, 2280, 2280, 1446, 2280, 1446,},
  150. { 3277, 2016, 3277, 2016, 2016, 1311, 2016, 1311, 3277, 2016, 3277, 2016, 2016, 1311, 2016, 1311,},
  151. { 2979, 1872, 2979, 1872, 1872, 1165, 1872, 1165, 2979, 1872, 2979, 1872, 1872, 1165, 1872, 1165,},
  152. { 2521, 1638, 2521, 1638, 1638, 1049, 1638, 1049, 2521, 1638, 2521, 1638, 1638, 1049, 1638, 1049,},
  153. { 2341, 1456, 2341, 1456, 1456, 912, 1456, 912, 2341, 1456, 2341, 1456, 1456, 912, 1456, 912,},
  154. { 2048, 1311, 2048, 1311, 1311, 839, 1311, 839, 2048, 1311, 2048, 1311, 1311, 839, 1311, 839,},
  155. { 1820, 1140, 1820, 1140, 1140, 723, 1140, 723, 1820, 1140, 1820, 1140, 1140, 723, 1140, 723,},
  156. { 1638, 1008, 1638, 1008, 1008, 655, 1008, 655, 1638, 1008, 1638, 1008, 1008, 655, 1008, 655,},
  157. { 1489, 936, 1489, 936, 936, 583, 936, 583, 1489, 936, 1489, 936, 936, 583, 936, 583,},
  158. { 1260, 819, 1260, 819, 819, 524, 819, 524, 1260, 819, 1260, 819, 819, 524, 819, 524,},
  159. { 1170, 728, 1170, 728, 728, 456, 728, 456, 1170, 728, 1170, 728, 728, 456, 728, 456,},
  160. };
  161. //FIXME need to check that this does not overflow signed 32 bit for low qp, I am not sure, it's very close
  162. //FIXME check that gcc inlines this (and optimizes intra & separate_dc stuff away)
  163. static inline int quantize_c(DCTELEM *block, uint8_t *scantable, int qscale,
  164. int intra, int separate_dc)
  165. {
  166. int i;
  167. const int * const quant_3Btable = quant_coeff[qscale];
  168. const int bias = intra ? (1 << QUANT_SHIFT) / 3 : (1 << QUANT_SHIFT) / 6;
  169. const unsigned int threshold1 = (1 << QUANT_SHIFT) - bias - 1;
  170. const unsigned int threshold2 = (threshold1 << 1);
  171. int last_non_zero;
  172. if (separate_dc) {
  173. if (qscale <= 18) {
  174. //avoid overflows
  175. const int dc_bias = intra ? (1 << (QUANT_SHIFT - 2)) / 3 : (1 << (QUANT_SHIFT - 2)) / 6;
  176. const unsigned int dc_threshold1 = (1 << (QUANT_SHIFT - 2)) - dc_bias - 1;
  177. const unsigned int dc_threshold2 = (dc_threshold1 << 1);
  178. int level = block[0]*quant_coeff[qscale+18][0];
  179. if (((unsigned)(level + dc_threshold1)) > dc_threshold2) {
  180. if (level > 0) {
  181. level = (dc_bias + level) >> (QUANT_SHIFT - 2);
  182. block[0] = level;
  183. } else {
  184. level = (dc_bias - level) >> (QUANT_SHIFT - 2);
  185. block[0] = -level;
  186. }
  187. // last_non_zero = i;
  188. } else {
  189. block[0] = 0;
  190. }
  191. } else {
  192. const int dc_bias = intra ? (1 << (QUANT_SHIFT + 1)) / 3 : (1 << (QUANT_SHIFT + 1)) / 6;
  193. const unsigned int dc_threshold1 = (1 << (QUANT_SHIFT + 1)) - dc_bias - 1;
  194. const unsigned int dc_threshold2 = (dc_threshold1 << 1);
  195. int level = block[0]*quant_table[0];
  196. if (((unsigned)(level + dc_threshold1)) > dc_threshold2) {
  197. if (level > 0) {
  198. level = (dc_bias + level) >> (QUANT_SHIFT + 1);
  199. block[0] = level;
  200. } else {
  201. level = (dc_bias - level) >> (QUANT_SHIFT + 1);
  202. block[0] = -level;
  203. }
  204. // last_non_zero = i;
  205. } else {
  206. block[0] = 0;
  207. }
  208. }
  209. last_non_zero = 0;
  210. i = 1;
  211. } else {
  212. last_non_zero = -1;
  213. i = 0;
  214. }
  215. for (; i < 16; i++) {
  216. const int j = scantable[i];
  217. int level = block[j]*quant_table[j];
  218. // if ( bias+level >= (1 << (QMAT_SHIFT - 3))
  219. // || bias-level >= (1 << (QMAT_SHIFT - 3))) {
  220. if (((unsigned)(level + threshold1)) > threshold2) {
  221. if (level > 0) {
  222. level = (bias + level) >> QUANT_SHIFT;
  223. block[j] = level;
  224. } else {
  225. level = (bias - level) >> QUANT_SHIFT;
  226. block[j] = -level;
  227. }
  228. last_non_zero = i;
  229. } else {
  230. block[j] = 0;
  231. }
  232. }
  233. return last_non_zero;
  234. }