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.

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