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.

417 lines
14KB

  1. /*
  2. * RTP H264 Protocol (RFC3984)
  3. * Copyright (c) 2006 Ryan Martell.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file rtp_h264.c
  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, FU-B packet types)
  32. *
  33. * @note TODO:
  34. * 1) RTCP sender reports for udp streams are required..
  35. *
  36. */
  37. #include "libavutil/base64.h"
  38. #include "libavutil/avstring.h"
  39. #include "libavcodec/bitstream.h"
  40. #include "avformat.h"
  41. #include "mpegts.h"
  42. #include <unistd.h>
  43. #include "network.h"
  44. #include <assert.h>
  45. #include "rtp_internal.h"
  46. #include "rtp_h264.h"
  47. /**
  48. RTP/H264 specific private data.
  49. */
  50. typedef struct h264_rtp_extra_data {
  51. unsigned long cookie; ///< sanity check, to make sure we get the pointer we're expecting.
  52. //sdp setup parameters
  53. uint8_t profile_idc; ///< from the sdp setup parameters.
  54. uint8_t profile_iop; ///< from the sdp setup parameters.
  55. uint8_t level_idc; ///< from the sdp setup parameters.
  56. int packetization_mode; ///< from the sdp setup parameters.
  57. #ifdef DEBUG
  58. int packet_types_received[32];
  59. #endif
  60. } h264_rtp_extra_data;
  61. #define MAGIC_COOKIE (0xdeadbeef) ///< Cookie for the extradata; to verify we are what we think we are, and that we haven't been freed.
  62. #define DEAD_COOKIE (0xdeaddead) ///< Cookie for the extradata; once it is freed.
  63. /* ---------------- private code */
  64. static void sdp_parse_fmtp_config_h264(AVStream * stream,
  65. h264_rtp_extra_data * h264_data,
  66. char *attr, char *value)
  67. {
  68. AVCodecContext *codec = stream->codec;
  69. assert(codec->codec_id == CODEC_ID_H264);
  70. assert(h264_data != NULL);
  71. if (!strcmp(attr, "packetization-mode")) {
  72. av_log(NULL, AV_LOG_DEBUG, "H.264/RTP Packetization Mode: %d\n", atoi(value));
  73. h264_data->packetization_mode = atoi(value);
  74. /*
  75. Packetization Mode:
  76. 0 or not present: Single NAL mode (Only nals from 1-23 are allowed)
  77. 1: Non-interleaved Mode: 1-23, 24 (STAP-A), 28 (FU-A) are allowed.
  78. 2: Interleaved Mode: 25 (STAP-B), 26 (MTAP16), 27 (MTAP24), 28 (FU-A), and 29 (FU-B) are allowed.
  79. */
  80. if (h264_data->packetization_mode > 1)
  81. av_log(stream, AV_LOG_ERROR,
  82. "H.264/RTP Interleaved RTP mode is not supported yet.");
  83. } else if (!strcmp(attr, "profile-level-id")) {
  84. if (strlen(value) == 6) {
  85. char buffer[3];
  86. // 6 characters=3 bytes, in hex.
  87. uint8_t profile_idc;
  88. uint8_t profile_iop;
  89. uint8_t level_idc;
  90. buffer[0] = value[0]; buffer[1] = value[1]; buffer[2] = '\0';
  91. profile_idc = strtol(buffer, NULL, 16);
  92. buffer[0] = value[2]; buffer[1] = value[3];
  93. profile_iop = strtol(buffer, NULL, 16);
  94. buffer[0] = value[4]; buffer[1] = value[5];
  95. level_idc = strtol(buffer, NULL, 16);
  96. // set the parameters...
  97. av_log(NULL, AV_LOG_DEBUG,
  98. "H.264/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. uint8_t start_sequence[]= { 0, 0, 1 };
  106. codec->extradata_size= 0;
  107. codec->extradata= NULL;
  108. while (*value) {
  109. char base64packet[1024];
  110. uint8_t decoded_packet[1024];
  111. uint32_t packet_size;
  112. char *dst = base64packet;
  113. while (*value && *value != ','
  114. && (dst - base64packet) < sizeof(base64packet) - 1) {
  115. *dst++ = *value++;
  116. }
  117. *dst++ = '\0';
  118. if (*value == ',')
  119. value++;
  120. packet_size= av_base64_decode(decoded_packet, base64packet, sizeof(decoded_packet));
  121. if (packet_size) {
  122. uint8_t *dest= av_malloc(packet_size+sizeof(start_sequence)+codec->extradata_size);
  123. if(dest)
  124. {
  125. if(codec->extradata_size)
  126. {
  127. // av_realloc?
  128. memcpy(dest, codec->extradata, codec->extradata_size);
  129. av_free(codec->extradata);
  130. }
  131. memcpy(dest+codec->extradata_size, start_sequence, sizeof(start_sequence));
  132. memcpy(dest+codec->extradata_size+sizeof(start_sequence), decoded_packet, packet_size);
  133. codec->extradata= dest;
  134. codec->extradata_size+= sizeof(start_sequence)+packet_size;
  135. } else {
  136. av_log(NULL, AV_LOG_ERROR, "H.264/RTP Unable to allocate memory for extradata!");
  137. }
  138. }
  139. }
  140. av_log(NULL, AV_LOG_DEBUG, "H.264/RTP Extradata set to %p (size: %d)!", codec->extradata, codec->extradata_size);
  141. }
  142. }
  143. // return 0 on packet, no more left, 1 on packet, 1 on partial packet...
  144. static int h264_handle_packet(RTPDemuxContext * s,
  145. AVPacket * pkt,
  146. uint32_t * timestamp,
  147. const uint8_t * buf,
  148. int len, int flags)
  149. {
  150. #ifdef DEBUG
  151. h264_rtp_extra_data *data = s->dynamic_protocol_context;
  152. #endif
  153. uint8_t nal = buf[0];
  154. uint8_t type = (nal & 0x1f);
  155. int result= 0;
  156. uint8_t start_sequence[]= {0, 0, 1};
  157. #ifdef DEBUG
  158. assert(data);
  159. assert(data->cookie == MAGIC_COOKIE);
  160. #endif
  161. assert(buf);
  162. if (type >= 1 && type <= 23)
  163. type = 1; // simplify the case. (these are all the nal types used internally by the h264 codec)
  164. switch (type) {
  165. case 0: // undefined;
  166. result= -1;
  167. break;
  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. do {
  189. uint16_t nal_size = AV_RB16(src); // this going to be a problem if unaligned (can it be?)
  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(NULL, 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(NULL, AV_LOG_ERROR,
  217. "Consumed more bytes than we got! (%d)\n", src_len);
  218. } while (src_len > 2); // because there could be rtp padding..
  219. if(pass==0) {
  220. // now we know the total size of the packet (with the start sequences added)
  221. av_new_packet(pkt, total_length);
  222. dst= pkt->data;
  223. } else {
  224. assert(dst-pkt->data==total_length);
  225. }
  226. }
  227. }
  228. break;
  229. case 25: // STAP-B
  230. case 26: // MTAP-16
  231. case 27: // MTAP-24
  232. case 29: // FU-B
  233. av_log(NULL, AV_LOG_ERROR,
  234. "Unhandled type (%d) (See RFC for implementation details\n",
  235. type);
  236. result= -1;
  237. break;
  238. case 28: // FU-A (fragmented nal)
  239. buf++;
  240. len--; // skip the fu_indicator
  241. {
  242. // these are the same as above, we just redo them here for clarity...
  243. uint8_t fu_indicator = nal;
  244. uint8_t fu_header = *buf; // read the fu_header.
  245. uint8_t start_bit = fu_header >> 7;
  246. // uint8_t end_bit = (fu_header & 0x40) >> 6;
  247. uint8_t nal_type = (fu_header & 0x1f);
  248. uint8_t reconstructed_nal;
  249. // reconstruct this packet's true nal; only the data follows..
  250. reconstructed_nal = fu_indicator & (0xe0); // the original nal forbidden bit and NRI are stored in this packet's nal;
  251. reconstructed_nal |= nal_type;
  252. // skip the fu_header...
  253. buf++;
  254. len--;
  255. #ifdef DEBUG
  256. if (start_bit)
  257. data->packet_types_received[nal_type]++;
  258. #endif
  259. if(start_bit) {
  260. // copy in the start sequence, and the reconstructed nal....
  261. av_new_packet(pkt, sizeof(start_sequence)+sizeof(nal)+len);
  262. memcpy(pkt->data, start_sequence, sizeof(start_sequence));
  263. pkt->data[sizeof(start_sequence)]= reconstructed_nal;
  264. memcpy(pkt->data+sizeof(start_sequence)+sizeof(nal), buf, len);
  265. } else {
  266. av_new_packet(pkt, len);
  267. memcpy(pkt->data, buf, len);
  268. }
  269. }
  270. break;
  271. case 30: // undefined
  272. case 31: // undefined
  273. default:
  274. av_log(NULL, AV_LOG_ERROR, "Undefined type (%d)", type);
  275. result= -1;
  276. break;
  277. }
  278. return result;
  279. }
  280. /* ---------------- public code */
  281. static void *h264_new_extradata(void)
  282. {
  283. h264_rtp_extra_data *data =
  284. av_mallocz(sizeof(h264_rtp_extra_data) +
  285. FF_INPUT_BUFFER_PADDING_SIZE);
  286. if (data) {
  287. data->cookie = MAGIC_COOKIE;
  288. }
  289. return data;
  290. }
  291. static void h264_free_extradata(void *d)
  292. {
  293. h264_rtp_extra_data *data = (h264_rtp_extra_data *) d;
  294. #ifdef DEBUG
  295. int ii;
  296. for (ii = 0; ii < 32; ii++) {
  297. if (data->packet_types_received[ii])
  298. av_log(NULL, AV_LOG_DEBUG, "Received %d packets of type %d\n",
  299. data->packet_types_received[ii], ii);
  300. }
  301. #endif
  302. assert(data);
  303. assert(data->cookie == MAGIC_COOKIE);
  304. // avoid stale pointers (assert)
  305. data->cookie = DEAD_COOKIE;
  306. // and clear out this...
  307. av_free(data);
  308. }
  309. static int parse_h264_sdp_line(AVStream * stream, void *data,
  310. const char *line)
  311. {
  312. AVCodecContext *codec = stream->codec;
  313. h264_rtp_extra_data *h264_data = (h264_rtp_extra_data *) data;
  314. const char *p = line;
  315. assert(h264_data->cookie == MAGIC_COOKIE);
  316. if (av_strstart(p, "framesize:", &p)) {
  317. char buf1[50];
  318. char *dst = buf1;
  319. // remove the protocol identifier..
  320. while (*p && *p == ' ') p++; // strip spaces.
  321. while (*p && *p != ' ') p++; // eat protocol identifier
  322. while (*p && *p == ' ') p++; // strip trailing spaces.
  323. while (*p && *p != '-' && (buf1 - dst) < sizeof(buf1) - 1) {
  324. *dst++ = *p++;
  325. }
  326. *dst = '\0';
  327. // a='framesize:96 320-240'
  328. // set our parameters..
  329. codec->width = atoi(buf1);
  330. codec->height = atoi(p + 1); // skip the -
  331. codec->pix_fmt = PIX_FMT_YUV420P;
  332. } else if (av_strstart(p, "fmtp:", &p)) {
  333. char attr[256];
  334. char value[4096];
  335. // remove the protocol identifier..
  336. while (*p && *p == ' ') p++; // strip spaces.
  337. while (*p && *p != ' ') p++; // eat protocol identifier
  338. while (*p && *p == ' ') p++; // strip trailing spaces.
  339. /* loop on each attribute */
  340. while (rtsp_next_attr_and_value
  341. (&p, attr, sizeof(attr), value, sizeof(value))) {
  342. /* grab the codec extra_data from the config parameter of the fmtp line */
  343. sdp_parse_fmtp_config_h264(stream, h264_data, attr, value);
  344. }
  345. } else if (av_strstart(p, "cliprect:", &p)) {
  346. // could use this if we wanted.
  347. }
  348. av_set_pts_info(stream, 33, 1, 90000); // 33 should be right, because the pts is 64 bit? (done elsewhere; this is a one time thing)
  349. return 0; // keep processing it the normal way...
  350. }
  351. /**
  352. This is the structure for expanding on the dynamic rtp protocols (makes everything static. yay!)
  353. */
  354. RTPDynamicProtocolHandler ff_h264_dynamic_handler = {
  355. "H264",
  356. CODEC_TYPE_VIDEO,
  357. CODEC_ID_H264,
  358. parse_h264_sdp_line,
  359. h264_new_extradata,
  360. h264_free_extradata,
  361. h264_handle_packet
  362. };