rtpdec_h264.c 13 KB

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