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.

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