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.

416 lines
13KB

  1. /*
  2. * RTP H264 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. 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. FF_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, FF_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. char *attr, char *value)
  130. {
  131. AVCodecContext *codec = stream->codec;
  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. codec->extradata_size = 0;
  151. av_freep(&codec->extradata);
  152. ret = ff_h264_parse_sprop_parameter_sets(s, &codec->extradata,
  153. &codec->extradata_size, value);
  154. av_log(s, AV_LOG_DEBUG, "Extradata set to %p (size: %d)\n",
  155. codec->extradata, codec->extradata_size);
  156. return ret;
  157. }
  158. return 0;
  159. }
  160. int ff_h264_handle_aggregated_packet(AVFormatContext *ctx, AVPacket *pkt,
  161. const uint8_t *buf, int len,
  162. int skip_between, int *nal_counters,
  163. int nal_mask)
  164. {
  165. int pass = 0;
  166. int total_length = 0;
  167. uint8_t *dst = NULL;
  168. int ret;
  169. // first we are going to figure out the total size
  170. for (pass = 0; pass < 2; pass++) {
  171. const uint8_t *src = buf;
  172. int src_len = len;
  173. while (src_len > 2) {
  174. uint16_t nal_size = AV_RB16(src);
  175. // consume the length of the aggregate
  176. src += 2;
  177. src_len -= 2;
  178. if (nal_size <= src_len) {
  179. if (pass == 0) {
  180. // counting
  181. total_length += sizeof(start_sequence) + nal_size;
  182. } else {
  183. // copying
  184. memcpy(dst, start_sequence, sizeof(start_sequence));
  185. dst += sizeof(start_sequence);
  186. memcpy(dst, src, nal_size);
  187. if (nal_counters)
  188. nal_counters[(*src) & nal_mask]++;
  189. dst += nal_size;
  190. }
  191. } else {
  192. av_log(ctx, AV_LOG_ERROR,
  193. "nal size exceeds length: %d %d\n", nal_size, src_len);
  194. return AVERROR_INVALIDDATA;
  195. }
  196. // eat what we handled
  197. src += nal_size + skip_between;
  198. src_len -= nal_size + skip_between;
  199. }
  200. if (pass == 0) {
  201. /* now we know the total size of the packet (with the
  202. * start sequences added) */
  203. if ((ret = av_new_packet(pkt, total_length)) < 0)
  204. return ret;
  205. dst = pkt->data;
  206. }
  207. }
  208. return 0;
  209. }
  210. static int h264_handle_packet_fu_a(AVFormatContext *ctx, AVPacket *pkt,
  211. const uint8_t *buf, int len,
  212. int *nal_counters, int nal_mask)
  213. {
  214. uint8_t fu_indicator, fu_header, start_bit, nal_type, nal;
  215. int ret;
  216. if (len < 3) {
  217. av_log(ctx, AV_LOG_ERROR, "Too short data for FU-A H264 RTP packet\n");
  218. return AVERROR_INVALIDDATA;
  219. }
  220. fu_indicator = buf[0];
  221. fu_header = buf[1];
  222. start_bit = fu_header >> 7;
  223. nal_type = fu_header & 0x1f;
  224. nal = fu_indicator & 0xe0 | nal_type;
  225. // skip the fu_indicator and fu_header
  226. buf += 2;
  227. len -= 2;
  228. if (start_bit) {
  229. if (nal_counters)
  230. nal_counters[nal_type & nal_mask]++;
  231. /* copy in the start sequence, and the reconstructed nal */
  232. if ((ret = av_new_packet(pkt, sizeof(start_sequence) + sizeof(nal) + len)) < 0)
  233. return ret;
  234. memcpy(pkt->data, start_sequence, sizeof(start_sequence));
  235. pkt->data[sizeof(start_sequence)] = nal;
  236. memcpy(pkt->data + sizeof(start_sequence) + sizeof(nal), buf, len);
  237. } else {
  238. if ((ret = av_new_packet(pkt, len)) < 0)
  239. return ret;
  240. memcpy(pkt->data, buf, len);
  241. }
  242. return 0;
  243. }
  244. // return 0 on packet, no more left, 1 on packet, 1 on partial packet
  245. static int h264_handle_packet(AVFormatContext *ctx, PayloadContext *data,
  246. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  247. const uint8_t *buf, int len, uint16_t seq,
  248. int flags)
  249. {
  250. uint8_t nal;
  251. uint8_t type;
  252. int result = 0;
  253. if (!len) {
  254. av_log(ctx, AV_LOG_ERROR, "Empty H264 RTP packet\n");
  255. return AVERROR_INVALIDDATA;
  256. }
  257. nal = buf[0];
  258. type = nal & 0x1f;
  259. /* Simplify the case (these are all the nal types used internally by
  260. * the h264 codec). */
  261. if (type >= 1 && type <= 23)
  262. type = 1;
  263. switch (type) {
  264. case 0: // undefined, but pass them through
  265. case 1:
  266. if ((result = av_new_packet(pkt, len + sizeof(start_sequence))) < 0)
  267. return result;
  268. memcpy(pkt->data, start_sequence, sizeof(start_sequence));
  269. memcpy(pkt->data + sizeof(start_sequence), buf, len);
  270. COUNT_NAL_TYPE(data, nal);
  271. break;
  272. case 24: // STAP-A (one packet, multiple nals)
  273. // consume the STAP-A NAL
  274. buf++;
  275. len--;
  276. result = ff_h264_handle_aggregated_packet(ctx, pkt, buf, len, 0,
  277. NAL_COUNTERS, NAL_MASK);
  278. break;
  279. case 25: // STAP-B
  280. case 26: // MTAP-16
  281. case 27: // MTAP-24
  282. case 29: // FU-B
  283. av_log(ctx, AV_LOG_ERROR,
  284. "Unhandled type (%d) (See RFC for implementation details)\n",
  285. type);
  286. result = AVERROR(ENOSYS);
  287. break;
  288. case 28: // FU-A (fragmented nal)
  289. result = h264_handle_packet_fu_a(ctx, pkt, buf, len,
  290. NAL_COUNTERS, NAL_MASK);
  291. break;
  292. case 30: // undefined
  293. case 31: // undefined
  294. default:
  295. av_log(ctx, AV_LOG_ERROR, "Undefined type (%d)\n", type);
  296. result = AVERROR_INVALIDDATA;
  297. break;
  298. }
  299. pkt->stream_index = st->index;
  300. return result;
  301. }
  302. static PayloadContext *h264_new_context(void)
  303. {
  304. return av_mallocz(sizeof(PayloadContext));
  305. }
  306. static void h264_free_context(PayloadContext *data)
  307. {
  308. #ifdef DEBUG
  309. int ii;
  310. for (ii = 0; ii < 32; ii++) {
  311. if (data->packet_types_received[ii])
  312. av_log(NULL, AV_LOG_DEBUG, "Received %d packets of type %d\n",
  313. data->packet_types_received[ii], ii);
  314. }
  315. #endif
  316. av_free(data);
  317. }
  318. static int parse_h264_sdp_line(AVFormatContext *s, int st_index,
  319. PayloadContext *h264_data, const char *line)
  320. {
  321. AVStream *stream;
  322. AVCodecContext *codec;
  323. const char *p = line;
  324. if (st_index < 0)
  325. return 0;
  326. stream = s->streams[st_index];
  327. codec = stream->codec;
  328. if (av_strstart(p, "framesize:", &p)) {
  329. char buf1[50];
  330. char *dst = buf1;
  331. // remove the protocol identifier
  332. while (*p && *p == ' ')
  333. p++; // strip spaces.
  334. while (*p && *p != ' ')
  335. p++; // eat protocol identifier
  336. while (*p && *p == ' ')
  337. p++; // strip trailing spaces.
  338. while (*p && *p != '-' && (dst - buf1) < sizeof(buf1) - 1)
  339. *dst++ = *p++;
  340. *dst = '\0';
  341. // a='framesize:96 320-240'
  342. // set our parameters
  343. codec->width = atoi(buf1);
  344. codec->height = atoi(p + 1); // skip the -
  345. } else if (av_strstart(p, "fmtp:", &p)) {
  346. return ff_parse_fmtp(s, stream, h264_data, p, sdp_parse_fmtp_config_h264);
  347. } else if (av_strstart(p, "cliprect:", &p)) {
  348. // could use this if we wanted.
  349. }
  350. return 0;
  351. }
  352. RTPDynamicProtocolHandler ff_h264_dynamic_handler = {
  353. .enc_name = "H264",
  354. .codec_type = AVMEDIA_TYPE_VIDEO,
  355. .codec_id = AV_CODEC_ID_H264,
  356. .need_parsing = AVSTREAM_PARSE_FULL,
  357. .parse_sdp_a_line = parse_h264_sdp_line,
  358. .alloc = h264_new_context,
  359. .free = h264_free_context,
  360. .parse_packet = h264_handle_packet,
  361. };