rtpdec_h264.c 13 KB

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