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.

430 lines
14KB

  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 "network.h"
  40. #include "rtpdec.h"
  41. #include "rtpdec_formats.h"
  42. struct PayloadContext {
  43. // sdp setup parameters
  44. uint8_t profile_idc;
  45. uint8_t profile_iop;
  46. uint8_t level_idc;
  47. int packetization_mode;
  48. #ifdef DEBUG
  49. int packet_types_received[32];
  50. #endif
  51. };
  52. #ifdef DEBUG
  53. #define COUNT_NAL_TYPE(data, nal) data->packet_types_received[(nal) & 0x1f]++
  54. #define NAL_COUNTERS data->packet_types_received
  55. #else
  56. #define COUNT_NAL_TYPE(data, nal) do { } while (0)
  57. #define NAL_COUNTERS NULL
  58. #endif
  59. #define NAL_MASK 0x1f
  60. static const uint8_t start_sequence[] = { 0, 0, 0, 1 };
  61. static void parse_profile_level_id(AVFormatContext *s,
  62. PayloadContext *h264_data,
  63. char *value)
  64. {
  65. char buffer[3];
  66. // 6 characters=3 bytes, in hex.
  67. uint8_t profile_idc;
  68. uint8_t profile_iop;
  69. uint8_t level_idc;
  70. buffer[0] = value[0];
  71. buffer[1] = value[1];
  72. buffer[2] = '\0';
  73. profile_idc = strtol(buffer, NULL, 16);
  74. buffer[0] = value[2];
  75. buffer[1] = value[3];
  76. profile_iop = strtol(buffer, NULL, 16);
  77. buffer[0] = value[4];
  78. buffer[1] = value[5];
  79. level_idc = strtol(buffer, NULL, 16);
  80. av_log(s, AV_LOG_DEBUG,
  81. "RTP Profile IDC: %x Profile IOP: %x Level: %x\n",
  82. profile_idc, profile_iop, level_idc);
  83. h264_data->profile_idc = profile_idc;
  84. h264_data->profile_iop = profile_iop;
  85. h264_data->level_idc = level_idc;
  86. }
  87. int ff_h264_parse_sprop_parameter_sets(AVFormatContext *s,
  88. uint8_t **data_ptr, int *size_ptr,
  89. const char *value)
  90. {
  91. char base64packet[1024];
  92. uint8_t decoded_packet[1024];
  93. int packet_size;
  94. while (*value) {
  95. char *dst = base64packet;
  96. while (*value && *value != ','
  97. && (dst - base64packet) < sizeof(base64packet) - 1) {
  98. *dst++ = *value++;
  99. }
  100. *dst++ = '\0';
  101. if (*value == ',')
  102. value++;
  103. packet_size = av_base64_decode(decoded_packet, base64packet,
  104. sizeof(decoded_packet));
  105. if (packet_size > 0) {
  106. uint8_t *dest = av_realloc(*data_ptr,
  107. packet_size + sizeof(start_sequence) +
  108. *size_ptr +
  109. FF_INPUT_BUFFER_PADDING_SIZE);
  110. if (!dest) {
  111. av_log(s, AV_LOG_ERROR,
  112. "Unable to allocate memory for extradata!\n");
  113. return AVERROR(ENOMEM);
  114. }
  115. *data_ptr = dest;
  116. memcpy(dest + *size_ptr, start_sequence,
  117. sizeof(start_sequence));
  118. memcpy(dest + *size_ptr + sizeof(start_sequence),
  119. decoded_packet, packet_size);
  120. memset(dest + *size_ptr + sizeof(start_sequence) +
  121. packet_size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  122. *size_ptr += sizeof(start_sequence) + packet_size;
  123. }
  124. }
  125. return 0;
  126. }
  127. static int sdp_parse_fmtp_config_h264(AVFormatContext *s,
  128. AVStream *stream,
  129. PayloadContext *h264_data,
  130. char *attr, char *value)
  131. {
  132. AVCodecContext *codec = stream->codec;
  133. if (!strcmp(attr, "packetization-mode")) {
  134. av_log(s, AV_LOG_DEBUG, "RTP Packetization Mode: %d\n", atoi(value));
  135. h264_data->packetization_mode = atoi(value);
  136. /*
  137. * Packetization Mode:
  138. * 0 or not present: Single NAL mode (Only nals from 1-23 are allowed)
  139. * 1: Non-interleaved Mode: 1-23, 24 (STAP-A), 28 (FU-A) are allowed.
  140. * 2: Interleaved Mode: 25 (STAP-B), 26 (MTAP16), 27 (MTAP24), 28 (FU-A),
  141. * and 29 (FU-B) are allowed.
  142. */
  143. if (h264_data->packetization_mode > 1)
  144. av_log(s, AV_LOG_ERROR,
  145. "Interleaved RTP mode is not supported yet.\n");
  146. } else if (!strcmp(attr, "profile-level-id")) {
  147. if (strlen(value) == 6)
  148. parse_profile_level_id(s, h264_data, value);
  149. } else if (!strcmp(attr, "sprop-parameter-sets")) {
  150. int ret;
  151. codec->extradata_size = 0;
  152. av_freep(&codec->extradata);
  153. ret = ff_h264_parse_sprop_parameter_sets(s, &codec->extradata,
  154. &codec->extradata_size, value);
  155. av_log(s, AV_LOG_DEBUG, "Extradata set to %p (size: %d)\n",
  156. codec->extradata, codec->extradata_size);
  157. return ret;
  158. }
  159. return 0;
  160. }
  161. int ff_h264_handle_aggregated_packet(AVFormatContext *ctx, AVPacket *pkt,
  162. const uint8_t *buf, int len,
  163. int start_skip, int *nal_counters,
  164. int nal_mask)
  165. {
  166. int pass = 0;
  167. int total_length = 0;
  168. uint8_t *dst = NULL;
  169. int ret;
  170. // first we are going to figure out the total size
  171. for (pass = 0; pass < 2; pass++) {
  172. const uint8_t *src = buf;
  173. int src_len = len;
  174. src += start_skip;
  175. src_len -= start_skip;
  176. while (src_len > 2) {
  177. uint16_t nal_size = AV_RB16(src);
  178. // consume the length of the aggregate
  179. src += 2;
  180. src_len -= 2;
  181. if (nal_size <= src_len) {
  182. if (pass == 0) {
  183. // counting
  184. total_length += sizeof(start_sequence) + nal_size;
  185. } else {
  186. // copying
  187. memcpy(dst, start_sequence, sizeof(start_sequence));
  188. dst += sizeof(start_sequence);
  189. memcpy(dst, src, nal_size);
  190. if (nal_counters)
  191. nal_counters[(*src) & nal_mask]++;
  192. dst += nal_size;
  193. }
  194. } else {
  195. av_log(ctx, AV_LOG_ERROR,
  196. "nal size exceeds length: %d %d\n", nal_size, src_len);
  197. return AVERROR_INVALIDDATA;
  198. }
  199. // eat what we handled
  200. src += nal_size + start_skip;
  201. src_len -= nal_size + start_skip;
  202. }
  203. if (pass == 0) {
  204. /* now we know the total size of the packet (with the
  205. * start sequences added) */
  206. if ((ret = av_new_packet(pkt, total_length)) < 0)
  207. return ret;
  208. dst = pkt->data;
  209. }
  210. }
  211. return 0;
  212. }
  213. static int h264_handle_packet_fu_a(AVFormatContext *ctx, AVPacket *pkt,
  214. const uint8_t *buf, int len,
  215. int *nal_counters, int nal_mask)
  216. {
  217. uint8_t fu_indicator, fu_header, start_bit, nal_type, nal;
  218. int ret;
  219. if (len < 3) {
  220. av_log(ctx, AV_LOG_ERROR, "Too short data for FU-A H264 RTP packet\n");
  221. return AVERROR_INVALIDDATA;
  222. }
  223. fu_indicator = buf[0];
  224. fu_header = buf[1];
  225. start_bit = fu_header >> 7;
  226. nal_type = fu_header & 0x1f;
  227. nal = fu_indicator & 0xe0 | nal_type;
  228. // skip the fu_indicator and fu_header
  229. buf += 2;
  230. len -= 2;
  231. if (start_bit) {
  232. if (nal_counters)
  233. nal_counters[nal_type & nal_mask]++;
  234. /* copy in the start sequence, and the reconstructed nal */
  235. if ((ret = av_new_packet(pkt, sizeof(start_sequence) + sizeof(nal) + len)) < 0)
  236. return ret;
  237. memcpy(pkt->data, start_sequence, sizeof(start_sequence));
  238. pkt->data[sizeof(start_sequence)] = nal;
  239. memcpy(pkt->data + sizeof(start_sequence) + sizeof(nal), buf, len);
  240. } else {
  241. if ((ret = av_new_packet(pkt, len)) < 0)
  242. return ret;
  243. memcpy(pkt->data, buf, len);
  244. }
  245. return 0;
  246. }
  247. // return 0 on packet, no more left, 1 on packet, 1 on partial packet
  248. static int h264_handle_packet(AVFormatContext *ctx, PayloadContext *data,
  249. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  250. const uint8_t *buf, int len, uint16_t seq,
  251. int flags)
  252. {
  253. uint8_t nal;
  254. uint8_t type;
  255. int result = 0;
  256. if (!len) {
  257. av_log(ctx, AV_LOG_ERROR, "Empty H264 RTP packet\n");
  258. return AVERROR_INVALIDDATA;
  259. }
  260. nal = buf[0];
  261. type = nal & 0x1f;
  262. /* Simplify the case (these are all the nal types used internally by
  263. * the h264 codec). */
  264. if (type >= 1 && type <= 23)
  265. type = 1;
  266. switch (type) {
  267. case 0: // undefined, but pass them through
  268. case 1:
  269. if ((result = av_new_packet(pkt, len + sizeof(start_sequence))) < 0)
  270. return result;
  271. memcpy(pkt->data, start_sequence, sizeof(start_sequence));
  272. memcpy(pkt->data + sizeof(start_sequence), buf, len);
  273. COUNT_NAL_TYPE(data, nal);
  274. break;
  275. case 24: // STAP-A (one packet, multiple nals)
  276. // consume the STAP-A NAL
  277. buf++;
  278. len--;
  279. result = ff_h264_handle_aggregated_packet(ctx, pkt, buf, len, 0,
  280. NAL_COUNTERS, NAL_MASK);
  281. break;
  282. case 25: // STAP-B
  283. case 26: // MTAP-16
  284. case 27: // MTAP-24
  285. case 29: // FU-B
  286. av_log(ctx, AV_LOG_ERROR,
  287. "Unhandled type (%d) (See RFC for implementation details)\n",
  288. type);
  289. result = AVERROR(ENOSYS);
  290. break;
  291. case 28: // FU-A (fragmented nal)
  292. result = h264_handle_packet_fu_a(ctx, pkt, buf, len,
  293. NAL_COUNTERS, NAL_MASK);
  294. break;
  295. case 30: // undefined
  296. case 31: // undefined
  297. default:
  298. av_log(ctx, AV_LOG_ERROR, "Undefined type (%d)\n", type);
  299. result = AVERROR_INVALIDDATA;
  300. break;
  301. }
  302. pkt->stream_index = st->index;
  303. return result;
  304. }
  305. static PayloadContext *h264_new_context(void)
  306. {
  307. return av_mallocz(sizeof(PayloadContext) + FF_INPUT_BUFFER_PADDING_SIZE);
  308. }
  309. static void h264_free_context(PayloadContext *data)
  310. {
  311. #ifdef DEBUG
  312. int ii;
  313. for (ii = 0; ii < 32; ii++) {
  314. if (data->packet_types_received[ii])
  315. av_log(NULL, AV_LOG_DEBUG, "Received %d packets of type %d\n",
  316. data->packet_types_received[ii], ii);
  317. }
  318. #endif
  319. av_free(data);
  320. }
  321. static av_cold int h264_init(AVFormatContext *s, int st_index,
  322. PayloadContext *data)
  323. {
  324. if (st_index < 0)
  325. return 0;
  326. s->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;
  327. return 0;
  328. }
  329. static int parse_h264_sdp_line(AVFormatContext *s, int st_index,
  330. PayloadContext *h264_data, const char *line)
  331. {
  332. AVStream *stream;
  333. AVCodecContext *codec;
  334. const char *p = line;
  335. if (st_index < 0)
  336. return 0;
  337. stream = s->streams[st_index];
  338. codec = stream->codec;
  339. if (av_strstart(p, "framesize:", &p)) {
  340. char buf1[50];
  341. char *dst = buf1;
  342. // remove the protocol identifier
  343. while (*p && *p == ' ')
  344. p++; // strip spaces.
  345. while (*p && *p != ' ')
  346. p++; // eat protocol identifier
  347. while (*p && *p == ' ')
  348. p++; // strip trailing spaces.
  349. while (*p && *p != '-' && (dst - buf1) < sizeof(buf1) - 1)
  350. *dst++ = *p++;
  351. *dst = '\0';
  352. // a='framesize:96 320-240'
  353. // set our parameters
  354. codec->width = atoi(buf1);
  355. codec->height = atoi(p + 1); // skip the -
  356. } else if (av_strstart(p, "fmtp:", &p)) {
  357. return ff_parse_fmtp(s, stream, h264_data, p, sdp_parse_fmtp_config_h264);
  358. } else if (av_strstart(p, "cliprect:", &p)) {
  359. // could use this if we wanted.
  360. }
  361. return 0;
  362. }
  363. RTPDynamicProtocolHandler ff_h264_dynamic_handler = {
  364. .enc_name = "H264",
  365. .codec_type = AVMEDIA_TYPE_VIDEO,
  366. .codec_id = AV_CODEC_ID_H264,
  367. .init = h264_init,
  368. .parse_sdp_a_line = parse_h264_sdp_line,
  369. .alloc = h264_new_context,
  370. .free = h264_free_context,
  371. .parse_packet = h264_handle_packet
  372. };