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.

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