/libavformat/rtpdec_h264.c

http://github.com/FFmpeg/FFmpeg · C · 420 lines · 309 code · 53 blank · 58 comment · 65 complexity · 3b080465a3e7316a0215679767a14f0c MD5 · raw file

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