/src/avc.c

https://code.google.com/ · C · 298 lines · 197 code · 28 blank · 73 comment · 63 complexity · aa737072c29cc1a0f38382dfff658dac MD5 · raw file

  1. /*
  2. $Id: avc.c 231 2011-06-27 13:46:19Z marc.noirot $
  3. FLV Metadata updater
  4. Copyright (C) 2007-2012 Marc Noirot <marc.noirot AT gmail.com>
  5. This file is part of FLVMeta.
  6. FLVMeta is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. FLVMeta is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with FLVMeta; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdlib.h>
  19. #include "avc.h"
  20. /**
  21. bit buffer handling
  22. */
  23. typedef struct __bit_buffer {
  24. byte * start;
  25. /*size_t size;*/
  26. byte * current;
  27. uint8 read_bits;
  28. } bit_buffer;
  29. static void skip_bits(bit_buffer * bb, size_t nbits) {
  30. bb->current = bb->current + ((nbits + bb->read_bits) / 8);
  31. bb->read_bits = (uint8)((bb->read_bits + nbits) % 8);
  32. }
  33. static uint8 get_bit(bit_buffer * bb) {
  34. uint8 ret;
  35. ret = (*(bb->current) >> (7 - bb->read_bits)) & 0x1;
  36. if (bb->read_bits == 7) {
  37. bb->read_bits = 0;
  38. bb->current++;
  39. }
  40. else {
  41. bb->read_bits++;
  42. }
  43. return ret;
  44. }
  45. static uint32 get_bits(bit_buffer * bb, size_t nbits) {
  46. uint32 i, ret;
  47. ret = 0;
  48. for (i = 0; i < nbits; i++) {
  49. ret = (ret << 1) + get_bit(bb);
  50. }
  51. return ret;
  52. }
  53. static uint32 exp_golomb_ue(bit_buffer * bb) {
  54. uint8 bit, significant_bits;
  55. significant_bits = 0;
  56. bit = get_bit(bb);
  57. while (bit == 0) {
  58. significant_bits++;
  59. bit = get_bit(bb);
  60. }
  61. return (1 << significant_bits) + get_bits(bb, significant_bits) - 1;
  62. }
  63. static sint32 exp_golomb_se(bit_buffer * bb) {
  64. sint32 ret;
  65. ret = exp_golomb_ue(bb);
  66. if ((ret & 0x1) == 0) {
  67. return -(ret >> 1);
  68. }
  69. else {
  70. return (ret + 1) >> 1;
  71. }
  72. }
  73. /* AVC type definitions */
  74. #define AVC_SEQUENCE_HEADER 0
  75. #define AVC_NALU 1
  76. #define AVC_END_OF_SEQUENCE 2
  77. typedef struct __AVCDecoderConfigurationRecord {
  78. uint8 configurationVersion;
  79. uint8 AVCProfileIndication;
  80. uint8 profile_compatibility;
  81. uint8 AVCLevelIndication;
  82. uint8 lengthSizeMinusOne;
  83. uint8 numOfSequenceParameterSets;
  84. } AVCDecoderConfigurationRecord;
  85. int read_avc_decoder_configuration_record(flv_stream * f, AVCDecoderConfigurationRecord * adcr) {
  86. if (flv_read_tag_body(f, &adcr->configurationVersion, 1) == 1
  87. && flv_read_tag_body(f, &adcr->AVCProfileIndication, 1) == 1
  88. && flv_read_tag_body(f, &adcr->profile_compatibility, 1) == 1
  89. && flv_read_tag_body(f, &adcr->AVCLevelIndication, 1) == 1
  90. && flv_read_tag_body(f, &adcr->lengthSizeMinusOne, 1) == 1
  91. && flv_read_tag_body(f, &adcr->numOfSequenceParameterSets, 1) == 1) {
  92. return FLV_OK;
  93. }
  94. else {
  95. return FLV_ERROR_EOF;
  96. }
  97. }
  98. static void parse_scaling_list(uint32 size, bit_buffer * bb) {
  99. uint32 last_scale, next_scale, i;
  100. sint32 delta_scale;
  101. last_scale = 8;
  102. next_scale = 8;
  103. for (i = 0; i < size; i++) {
  104. if (next_scale != 0) {
  105. delta_scale = exp_golomb_se(bb);
  106. next_scale = (last_scale + delta_scale + 256) % 256;
  107. }
  108. if (next_scale != 0) {
  109. last_scale = next_scale;
  110. }
  111. }
  112. }
  113. /**
  114. Parses a SPS NALU to retrieve video width and height
  115. */
  116. static void parse_sps(byte * sps, size_t sps_size, uint32 * width, uint32 * height) {
  117. bit_buffer bb;
  118. uint32 profile, pic_order_cnt_type, width_in_mbs, height_in_map_units;
  119. uint32 i, size, left, right, top, bottom;
  120. uint8 frame_mbs_only_flag;
  121. bb.start = sps;
  122. /*bb.size = sps_size;*/
  123. bb.current = sps;
  124. bb.read_bits = 0;
  125. /* skip first byte, since we already know we're parsing a SPS */
  126. skip_bits(&bb, 8);
  127. /* get profile */
  128. profile = get_bits(&bb, 8);
  129. /* skip 4 bits + 4 zeroed bits + 8 bits = 16 bits = 2 bytes */
  130. skip_bits(&bb, 16);
  131. /* read sps id, first exp-golomb encoded value */
  132. exp_golomb_ue(&bb);
  133. if (profile == 100 || profile == 110 || profile == 122 || profile == 144) {
  134. /* chroma format idx */
  135. if (exp_golomb_ue(&bb) == 3) {
  136. skip_bits(&bb, 1);
  137. }
  138. /* bit depth luma minus8 */
  139. exp_golomb_ue(&bb);
  140. /* bit depth chroma minus8 */
  141. exp_golomb_ue(&bb);
  142. /* Qpprime Y Zero Transform Bypass flag */
  143. skip_bits(&bb, 1);
  144. /* Seq Scaling Matrix Present Flag */
  145. if (get_bit(&bb)) {
  146. for (i = 0; i < 8; i++) {
  147. /* Seq Scaling List Present Flag */
  148. if (get_bit(&bb)) {
  149. parse_scaling_list(i < 6 ? 16 : 64, &bb);
  150. }
  151. }
  152. }
  153. }
  154. /* log2_max_frame_num_minus4 */
  155. exp_golomb_ue(&bb);
  156. /* pic_order_cnt_type */
  157. pic_order_cnt_type = exp_golomb_ue(&bb);
  158. if (pic_order_cnt_type == 0) {
  159. /* log2_max_pic_order_cnt_lsb_minus4 */
  160. exp_golomb_ue(&bb);
  161. }
  162. else if (pic_order_cnt_type == 1) {
  163. /* delta_pic_order_always_zero_flag */
  164. skip_bits(&bb, 1);
  165. /* offset_for_non_ref_pic */
  166. exp_golomb_se(&bb);
  167. /* offset_for_top_to_bottom_field */
  168. exp_golomb_se(&bb);
  169. size = exp_golomb_ue(&bb);
  170. for (i = 0; i < size; i++) {
  171. /* offset_for_ref_frame */
  172. exp_golomb_se(&bb);
  173. }
  174. }
  175. /* num_ref_frames */
  176. exp_golomb_ue(&bb);
  177. /* gaps_in_frame_num_value_allowed_flag */
  178. skip_bits(&bb, 1);
  179. /* pic_width_in_mbs */
  180. width_in_mbs = exp_golomb_ue(&bb) + 1;
  181. /* pic_height_in_map_units */
  182. height_in_map_units = exp_golomb_ue(&bb) + 1;
  183. /* frame_mbs_only_flag */
  184. frame_mbs_only_flag = get_bit(&bb);
  185. if (!frame_mbs_only_flag) {
  186. /* mb_adaptive_frame_field */
  187. skip_bits(&bb, 1);
  188. }
  189. /* direct_8x8_inference_flag */
  190. skip_bits(&bb, 1);
  191. /* frame_cropping */
  192. left = right = top = bottom = 0;
  193. if (get_bit(&bb)) {
  194. left = exp_golomb_ue(&bb) * 2;
  195. right = exp_golomb_ue(&bb) * 2;
  196. top = exp_golomb_ue(&bb) * 2;
  197. bottom = exp_golomb_ue(&bb) * 2;
  198. if (!frame_mbs_only_flag) {
  199. top *= 2;
  200. bottom *= 2;
  201. }
  202. }
  203. /* width */
  204. *width = width_in_mbs * 16 - (left + right);
  205. /* height */
  206. *height = height_in_map_units * 16 - (top + bottom);
  207. if (!frame_mbs_only_flag) {
  208. *height *= 2;
  209. }
  210. }
  211. /**
  212. Tries to read the resolution of the current video packet.
  213. We assume to be at the first byte of the video data.
  214. */
  215. int read_avc_resolution(flv_stream * f, uint32 body_length, uint32 * width, uint32 * height) {
  216. byte avc_packet_type;
  217. uint24 composition_time;
  218. AVCDecoderConfigurationRecord adcr;
  219. uint16 sps_size;
  220. byte * sps_buffer;
  221. /* make sure we have enough bytes to read in the current tag */
  222. if (body_length < sizeof(byte) + sizeof(uint24) + sizeof(AVCDecoderConfigurationRecord)) {
  223. return FLV_OK;
  224. }
  225. /* determine whether we're reading an AVCDecoderConfigurationRecord */
  226. if (flv_read_tag_body(f, &avc_packet_type, 1) < 1) {
  227. return FLV_ERROR_EOF;
  228. }
  229. if (avc_packet_type != AVC_SEQUENCE_HEADER) {
  230. return FLV_OK;
  231. }
  232. /* read the composition time */
  233. if (flv_read_tag_body(f, &composition_time, sizeof(uint24)) < sizeof(uint24)) {
  234. return FLV_ERROR_EOF;
  235. }
  236. /* we need to read an AVCDecoderConfigurationRecord */
  237. if (read_avc_decoder_configuration_record(f, &adcr) == FLV_ERROR_EOF) {
  238. return FLV_ERROR_EOF;
  239. }
  240. /* number of SequenceParameterSets */
  241. if ((adcr.numOfSequenceParameterSets & 0x1F) == 0) {
  242. /* no SPS, return */
  243. return FLV_OK;
  244. }
  245. /** read the first SequenceParameterSet found */
  246. /* SPS size */
  247. if (flv_read_tag_body(f, &sps_size, sizeof(uint16)) < sizeof(uint16)) {
  248. return FLV_ERROR_EOF;
  249. }
  250. sps_size = swap_uint16(sps_size);
  251. /* read the SPS entirely */
  252. sps_buffer = (byte *) malloc((size_t)sps_size);
  253. if (sps_buffer == NULL) {
  254. return FLV_ERROR_MEMORY;
  255. }
  256. if (flv_read_tag_body(f, sps_buffer, (size_t)sps_size) < (size_t)sps_size) {
  257. free(sps_buffer);
  258. return FLV_ERROR_EOF;
  259. }
  260. /* parse SPS to determine video resolution */
  261. parse_sps(sps_buffer, (size_t)sps_size, width, height);
  262. free(sps_buffer);
  263. return FLV_OK;
  264. }