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.

157 lines
4.8KB

  1. /*
  2. * RTP parser for DV payload format (RFC 6469)
  3. * Copyright (c) 2015 Thomas Volkert <thomas@homer-conferencing.com>
  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. #include "libavutil/avstring.h"
  22. #include "libavcodec/bytestream.h"
  23. #include "rtpdec_formats.h"
  24. struct PayloadContext {
  25. AVIOContext *buf;
  26. uint32_t timestamp;
  27. int bundled_audio;
  28. };
  29. static av_cold PayloadContext *dv_new_context(void)
  30. {
  31. return av_mallocz(sizeof(PayloadContext));
  32. }
  33. static void dv_free_dyn_buffer(AVIOContext **dyn_buf)
  34. {
  35. uint8_t *ptr_dyn_buffer;
  36. avio_close_dyn_buf(*dyn_buf, &ptr_dyn_buffer);
  37. av_free(ptr_dyn_buffer);
  38. *dyn_buf = NULL;
  39. }
  40. static av_cold void dv_free_context(PayloadContext *data)
  41. {
  42. dv_free_dyn_buffer(&data->buf);
  43. av_free(data);
  44. }
  45. static av_cold int dv_sdp_parse_fmtp_config(AVFormatContext *s,
  46. AVStream *stream,
  47. PayloadContext *dv_data,
  48. char *attr, char *value)
  49. {
  50. /* does the DV stream include audio? */
  51. if (!strcmp(attr, "audio") && !strcmp(value, "bundled"))
  52. dv_data->bundled_audio = 1;
  53. /* extract the DV profile */
  54. if (!strcmp(attr, "encode")) {
  55. /* SD-VCR/525-60 */
  56. /* SD-VCR/625-50 */
  57. /* HD-VCR/1125-60 */
  58. /* HD-VCR/1250-50 */
  59. /* SDL-VCR/525-60 */
  60. /* SDL-VCR/625-50 */
  61. /* 314M-25/525-60 */
  62. /* 314M-25/625-50 */
  63. /* 314M-50/525-60 */
  64. /* 314M-50/625-50 */
  65. /* 370M/1080-60i */
  66. /* 370M/1080-50i */
  67. /* 370M/720-60p */
  68. /* 370M/720-50p */
  69. /* 306M/525-60 (for backward compatibility) */
  70. /* 306M/625-50 (for backward compatibility) */
  71. }
  72. return 0;
  73. }
  74. static av_cold int dv_parse_sdp_line(AVFormatContext *ctx, int st_index,
  75. PayloadContext *dv_data, const char *line)
  76. {
  77. AVStream *current_stream;
  78. const char *sdp_line_ptr = line;
  79. if (st_index < 0)
  80. return 0;
  81. current_stream = ctx->streams[st_index];
  82. if (av_strstart(sdp_line_ptr, "fmtp:", &sdp_line_ptr)) {
  83. return ff_parse_fmtp(ctx, current_stream, dv_data, sdp_line_ptr,
  84. dv_sdp_parse_fmtp_config);
  85. }
  86. return 0;
  87. }
  88. static int dv_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_dv_ctx,
  89. AVStream *st, AVPacket *pkt, uint32_t *timestamp,
  90. const uint8_t *buf, int len, uint16_t seq,
  91. int flags)
  92. {
  93. int res = 0;
  94. /* drop data of previous packets in case of non-continuous (lossy) packet stream */
  95. if (rtp_dv_ctx->buf && rtp_dv_ctx->timestamp != *timestamp) {
  96. dv_free_dyn_buffer(&rtp_dv_ctx->buf);
  97. }
  98. /* sanity check for size of input packet: 1 byte payload at least */
  99. if (len < 1) {
  100. av_log(ctx, AV_LOG_ERROR, "Too short RTP/DV packet, got %d bytes\n", len);
  101. return AVERROR_INVALIDDATA;
  102. }
  103. /* start frame buffering with new dynamic buffer */
  104. if (!rtp_dv_ctx->buf) {
  105. res = avio_open_dyn_buf(&rtp_dv_ctx->buf);
  106. if (res < 0)
  107. return res;
  108. /* update the timestamp in the frame packet with the one from the RTP packet */
  109. rtp_dv_ctx->timestamp = *timestamp;
  110. }
  111. /* write the fragment to the dyn. buffer */
  112. avio_write(rtp_dv_ctx->buf, buf, len);
  113. /* RTP marker bit means: last fragment of current frame was received;
  114. otherwise, an additional fragment is needed for the current frame */
  115. if (!(flags & RTP_FLAG_MARKER))
  116. return AVERROR(EAGAIN);
  117. /* close frame buffering and create resulting A/V packet */
  118. res = ff_rtp_finalize_packet(pkt, &rtp_dv_ctx->buf, st->index);
  119. if (res < 0)
  120. return res;
  121. return 0;
  122. }
  123. RTPDynamicProtocolHandler ff_dv_dynamic_handler = {
  124. .enc_name = "DV",
  125. .codec_type = AVMEDIA_TYPE_VIDEO,
  126. .codec_id = AV_CODEC_ID_DVVIDEO,
  127. .need_parsing = AVSTREAM_PARSE_FULL,
  128. .parse_sdp_a_line = dv_parse_sdp_line,
  129. .alloc = dv_new_context,
  130. .free = dv_free_context,
  131. .parse_packet = dv_handle_packet,
  132. };