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.

417 lines
13KB

  1. /*
  2. * RTP H.264 Protocol (RFC3984)
  3. * Copyright (c) 2006 Ryan Martell
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * @brief H.264 / RTP Code (RFC3984)
  24. * @author Ryan Martell <rdm4@martellventures.com>
  25. *
  26. * @note Notes:
  27. * Notes:
  28. * This currently supports packetization mode:
  29. * Single Nal Unit Mode (0), or
  30. * Non-Interleaved Mode (1). It currently does not support
  31. * Interleaved Mode (2). (This requires implementing STAP-B, MTAP16, MTAP24,
  32. * FU-B packet types)
  33. */
  34. #include "libavutil/attributes.h"
  35. #include "libavutil/base64.h"
  36. #include "libavutil/intreadwrite.h"
  37. #include "libavutil/avstring.h"
  38. #include "avformat.h"
  39. #include "rtpdec.h"
  40. #include "rtpdec_formats.h"
  41. struct PayloadContext {
  42. // sdp setup parameters
  43. uint8_t profile_idc;
  44. uint8_t profile_iop;
  45. uint8_t level_idc;
  46. int packetization_mode;
  47. #ifdef DEBUG
  48. int packet_types_received[32];
  49. #endif
  50. };
  51. #ifdef DEBUG
  52. #define COUNT_NAL_TYPE(data, nal) data->packet_types_received[(nal) & 0x1f]++
  53. #define NAL_COUNTERS data->packet_types_received
  54. #else
  55. #define COUNT_NAL_TYPE(data, nal) do { } while (0)
  56. #define NAL_COUNTERS NULL
  57. #endif
  58. #define NAL_MASK 0x1f
  59. static const uint8_t start_sequence[] = { 0, 0, 0, 1 };
  60. static void parse_profile_level_id(AVFormatContext *s,
  61. PayloadContext *h264_data,
  62. const char *value)
  63. {
  64. char buffer[3];
  65. // 6 characters=3 bytes, in hex.
  66. uint8_t profile_idc;
  67. uint8_t profile_iop;
  68. uint8_t level_idc;
  69. buffer[0] = value[0];
  70. buffer[1] = value[1];
  71. buffer[2] = '\0';
  72. profile_idc = strtol(buffer, NULL, 16);
  73. buffer[0] = value[2];
  74. buffer[1] = value[3];
  75. profile_iop = strtol(buffer, NULL, 16);
  76. buffer[0] = value[4];
  77. buffer[1] = value[5];
  78. level_idc = strtol(buffer, NULL, 16);
  79. av_log(s, AV_LOG_DEBUG,
  80. "RTP Profile IDC: %x Profile IOP: %x Level: %x\n",
  81. profile_idc, profile_iop, level_idc);
  82. h264_data->profile_idc = profile_idc;
  83. h264_data->profile_iop = profile_iop;
  84. h264_data->level_idc = level_idc;
  85. }
  86. int ff_h264_parse_sprop_parameter_sets(AVFormatContext *s,
  87. uint8_t **data_ptr, int *size_ptr,
  88. const char *value)
  89. {
  90. char base64packet[1024];
  91. uint8_t decoded_packet[1024];
  92. int packet_size;
  93. while (*value) {
  94. char *dst = base64packet;
  95. while (*value && *value != ','
  96. && (dst - base64packet) < sizeof(base64packet) - 1) {
  97. *dst++ = *value++;
  98. }
  99. *dst++ = '\0';
  100. if (*value == ',')
  101. value++;
  102. packet_size = av_base64_decode(decoded_packet, base64packet,
  103. sizeof(decoded_packet));
  104. if (packet_size > 0) {
  105. uint8_t *dest = av_realloc(*data_ptr,
  106. packet_size + sizeof(start_sequence) +
  107. *size_ptr +
  108. AV_INPUT_BUFFER_PADDING_SIZE);
  109. if (!dest) {
  110. av_log(s, AV_LOG_ERROR,
  111. "Unable to allocate memory for extradata!\n");
  112. return AVERROR(ENOMEM);
  113. }
  114. *data_ptr = dest;
  115. memcpy(dest + *size_ptr, start_sequence,
  116. sizeof(start_sequence));
  117. memcpy(dest + *size_ptr + sizeof(start_sequence),
  118. decoded_packet, packet_size);
  119. memset(dest + *size_ptr + sizeof(start_sequence) +
  120. packet_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  121. *size_ptr += sizeof(start_sequence) + packet_size;
  122. }
  123. }
  124. return 0;
  125. }
  126. static int sdp_parse_fmtp_config_h264(AVFormatContext *s,
  127. AVStream *stream,
  128. PayloadContext *h264_data,
  129. const char *attr, const char *value)
  130. {
  131. AVCodecParameters *par = stream->codecpar;
  132. if (!strcmp(attr, "packetization-mode")) {
  133. av_log(s, AV_LOG_DEBUG, "RTP Packetization Mode: %d\n", atoi(value));
  134. h264_data->packetization_mode = atoi(value);
  135. /*
  136. * Packetization Mode:
  137. * 0 or not present: Single NAL mode (Only nals from 1-23 are allowed)
  138. * 1: Non-interleaved Mode: 1-23, 24 (STAP-A), 28 (FU-A) are allowed.
  139. * 2: Interleaved Mode: 25 (STAP-B), 26 (MTAP16), 27 (MTAP24), 28 (FU-A),
  140. * and 29 (FU-B) are allowed.
  141. */
  142. if (h264_data->packetization_mode > 1)
  143. av_log(s, AV_LOG_ERROR,
  144. "Interleaved RTP mode is not supported yet.\n");
  145. } else if (!strcmp(attr, "profile-level-id")) {
  146. if (strlen(value) == 6)
  147. parse_profile_level_id(s, h264_data, value);
  148. } else if (!strcmp(attr, "sprop-parameter-sets")) {
  149. int ret;
  150. par->extradata_size = 0;
  151. av_freep(&par->extradata);
  152. ret = ff_h264_parse_sprop_parameter_sets(s, &par->extradata,
  153. &par->extradata_size, value);
  154. av_log(s, AV_LOG_DEBUG, "Extradata set to %p (size: %d)\n",
  155. par->extradata, par->extradata_size);
  156. return ret;
  157. }
  158. return 0;
  159. }
  160. void ff_h264_parse_framesize(AVCodecParameters *par, const char *p)
  161. {
  162. char buf1[50];
  163. char *dst = buf1;
  164. // remove the protocol identifier
  165. while (*p && *p == ' ')
  166. p++; // strip spaces.
  167. while (*p && *p != ' ')
  168. p++; // eat protocol identifier
  169. while (*p && *p == ' ')
  170. p++; // strip trailing spaces.
  171. while (*p && *p != '-' && (dst - buf1) < sizeof(buf1) - 1)
  172. *dst++ = *p++;
  173. *dst = '\0';
  174. // a='framesize:96 320-240'
  175. // set our parameters
  176. par->width = atoi(buf1);
  177. par->height = atoi(p + 1); // skip the -
  178. }
  179. int ff_h264_handle_aggregated_packet(AVFormatContext *ctx, AVPacket *pkt,
  180. const uint8_t *buf, int len,
  181. int skip_between, int *nal_counters,
  182. int nal_mask)
  183. {
  184. int pass = 0;
  185. int total_length = 0;
  186. uint8_t *dst = NULL;
  187. int ret;
  188. // first we are going to figure out the total size
  189. for (pass = 0; pass < 2; pass++) {
  190. const uint8_t *src = buf;
  191. int src_len = len;
  192. while (src_len > 2) {
  193. uint16_t nal_size = AV_RB16(src);
  194. // consume the length of the aggregate
  195. src += 2;
  196. src_len -= 2;
  197. if (nal_size <= src_len) {
  198. if (pass == 0) {
  199. // counting
  200. total_length += sizeof(start_sequence) + nal_size;
  201. } else {
  202. // copying
  203. memcpy(dst, start_sequence, sizeof(start_sequence));
  204. dst += sizeof(start_sequence);
  205. memcpy(dst, src, nal_size);
  206. if (nal_counters)
  207. nal_counters[(*src) & nal_mask]++;
  208. dst += nal_size;
  209. }
  210. } else {
  211. av_log(ctx, AV_LOG_ERROR,
  212. "nal size exceeds length: %d %d\n", nal_size, src_len);
  213. return AVERROR_INVALIDDATA;
  214. }
  215. // eat what we handled
  216. src += nal_size + skip_between;
  217. src_len -= nal_size + skip_between;
  218. }
  219. if (pass == 0) {
  220. /* now we know the total size of the packet (with the
  221. * start sequences added) */
  222. if ((ret = av_new_packet(pkt, total_length)) < 0)
  223. return ret;
  224. dst = pkt->data;
  225. }
  226. }
  227. return 0;
  228. }
  229. int ff_h264_handle_frag_packet(AVPacket *pkt, const uint8_t *buf, int len,
  230. int start_bit, const uint8_t *nal_header,
  231. int nal_header_len)
  232. {
  233. int ret;
  234. int tot_len = len;
  235. int pos = 0;
  236. if (start_bit)
  237. tot_len += sizeof(start_sequence) + nal_header_len;
  238. if ((ret = av_new_packet(pkt, tot_len)) < 0)
  239. return ret;
  240. if (start_bit) {
  241. memcpy(pkt->data + pos, start_sequence, sizeof(start_sequence));
  242. pos += sizeof(start_sequence);
  243. memcpy(pkt->data + pos, nal_header, nal_header_len);
  244. pos += nal_header_len;
  245. }
  246. memcpy(pkt->data + pos, buf, len);
  247. return 0;
  248. }
  249. static int h264_handle_packet_fu_a(AVFormatContext *ctx, AVPacket *pkt,
  250. const uint8_t *buf, int len,
  251. int *nal_counters, int nal_mask)
  252. {
  253. uint8_t fu_indicator, fu_header, start_bit, nal_type, nal;
  254. if (len < 3) {
  255. av_log(ctx, AV_LOG_ERROR, "Too short data for FU-A H.264 RTP packet\n");
  256. return AVERROR_INVALIDDATA;
  257. }
  258. fu_indicator = buf[0];
  259. fu_header = buf[1];
  260. start_bit = fu_header >> 7;
  261. nal_type = fu_header & 0x1f;
  262. nal = fu_indicator & 0xe0 | nal_type;
  263. // skip the fu_indicator and fu_header
  264. buf += 2;
  265. len -= 2;
  266. if (start_bit && nal_counters)
  267. nal_counters[nal_type & nal_mask]++;
  268. return ff_h264_handle_frag_packet(pkt, buf, len, start_bit, &nal, 1);
  269. }
  270. // return 0 on packet, no more left, 1 on packet, 1 on partial packet
  271. static int h264_handle_packet(AVFormatContext *ctx, PayloadContext *data,
  272. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  273. const uint8_t *buf, int len, uint16_t seq,
  274. int flags)
  275. {
  276. uint8_t nal;
  277. uint8_t type;
  278. int result = 0;
  279. if (!len) {
  280. av_log(ctx, AV_LOG_ERROR, "Empty H.264 RTP packet\n");
  281. return AVERROR_INVALIDDATA;
  282. }
  283. nal = buf[0];
  284. type = nal & 0x1f;
  285. /* Simplify the case (these are all the NAL types used internally by
  286. * the H.264 codec). */
  287. if (type >= 1 && type <= 23)
  288. type = 1;
  289. switch (type) {
  290. case 0: // undefined, but pass them through
  291. case 1:
  292. if ((result = av_new_packet(pkt, len + sizeof(start_sequence))) < 0)
  293. return result;
  294. memcpy(pkt->data, start_sequence, sizeof(start_sequence));
  295. memcpy(pkt->data + sizeof(start_sequence), buf, len);
  296. COUNT_NAL_TYPE(data, nal);
  297. break;
  298. case 24: // STAP-A (one packet, multiple nals)
  299. // consume the STAP-A NAL
  300. buf++;
  301. len--;
  302. result = ff_h264_handle_aggregated_packet(ctx, pkt, buf, len, 0,
  303. NAL_COUNTERS, NAL_MASK);
  304. break;
  305. case 25: // STAP-B
  306. case 26: // MTAP-16
  307. case 27: // MTAP-24
  308. case 29: // FU-B
  309. avpriv_report_missing_feature(ctx, "RTP H.264 NAL unit type %d", type);
  310. result = AVERROR_PATCHWELCOME;
  311. break;
  312. case 28: // FU-A (fragmented nal)
  313. result = h264_handle_packet_fu_a(ctx, pkt, buf, len,
  314. NAL_COUNTERS, NAL_MASK);
  315. break;
  316. case 30: // undefined
  317. case 31: // undefined
  318. default:
  319. av_log(ctx, AV_LOG_ERROR, "Undefined type (%d)\n", type);
  320. result = AVERROR_INVALIDDATA;
  321. break;
  322. }
  323. pkt->stream_index = st->index;
  324. return result;
  325. }
  326. static void h264_close_context(PayloadContext *data)
  327. {
  328. #ifdef DEBUG
  329. int ii;
  330. for (ii = 0; ii < 32; ii++) {
  331. if (data->packet_types_received[ii])
  332. av_log(NULL, AV_LOG_DEBUG, "Received %d packets of type %d\n",
  333. data->packet_types_received[ii], ii);
  334. }
  335. #endif
  336. }
  337. static int parse_h264_sdp_line(AVFormatContext *s, int st_index,
  338. PayloadContext *h264_data, const char *line)
  339. {
  340. AVStream *stream;
  341. const char *p = line;
  342. if (st_index < 0)
  343. return 0;
  344. stream = s->streams[st_index];
  345. if (av_strstart(p, "framesize:", &p)) {
  346. ff_h264_parse_framesize(stream->codecpar, p);
  347. } else if (av_strstart(p, "fmtp:", &p)) {
  348. return ff_parse_fmtp(s, stream, h264_data, p, sdp_parse_fmtp_config_h264);
  349. } else if (av_strstart(p, "cliprect:", &p)) {
  350. // could use this if we wanted.
  351. }
  352. return 0;
  353. }
  354. RTPDynamicProtocolHandler ff_h264_dynamic_handler = {
  355. .enc_name = "H264",
  356. .codec_type = AVMEDIA_TYPE_VIDEO,
  357. .codec_id = AV_CODEC_ID_H264,
  358. .need_parsing = AVSTREAM_PARSE_FULL,
  359. .priv_data_size = sizeof(PayloadContext),
  360. .parse_sdp_a_line = parse_h264_sdp_line,
  361. .close = h264_close_context,
  362. .parse_packet = h264_handle_packet,
  363. };