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.

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