rtp_aac.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * copyright (c) 2007 Luca Abeni
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "avformat.h"
  21. #include "rtpenc.h"
  22. #define MAX_FRAMES_PER_PACKET (s->max_frames_per_packet ? s->max_frames_per_packet : 5)
  23. #define MAX_AU_HEADERS_SIZE (2 + 2 * MAX_FRAMES_PER_PACKET)
  24. void ff_rtp_send_aac(AVFormatContext *s1, const uint8_t *buff, int size)
  25. {
  26. RTPMuxContext *s = s1->priv_data;
  27. int len, max_packet_size;
  28. uint8_t *p;
  29. /* skip ADTS header, if present */
  30. if ((s1->streams[0]->codec->extradata_size) == 0) {
  31. size -= 7;
  32. buff += 7;
  33. }
  34. max_packet_size = s->max_payload_size - MAX_AU_HEADERS_SIZE;
  35. /* test if the packet must be sent */
  36. len = (s->buf_ptr - s->buf);
  37. if ((s->num_frames == MAX_FRAMES_PER_PACKET) || (len && (len + size) > max_packet_size)) {
  38. int au_size = s->num_frames * 2;
  39. p = s->buf + MAX_AU_HEADERS_SIZE - au_size - 2;
  40. if (p != s->buf) {
  41. memmove(p + 2, s->buf + 2, au_size);
  42. }
  43. /* Write the AU header size */
  44. p[0] = ((au_size * 8) & 0xFF) >> 8;
  45. p[1] = (au_size * 8) & 0xFF;
  46. ff_rtp_send_data(s1, p, s->buf_ptr - p, 1);
  47. s->num_frames = 0;
  48. }
  49. if (s->num_frames == 0) {
  50. s->buf_ptr = s->buf + MAX_AU_HEADERS_SIZE;
  51. s->timestamp = s->cur_timestamp;
  52. }
  53. if (size < max_packet_size) {
  54. p = s->buf + s->num_frames++ * 2 + 2;
  55. *p++ = size >> 5;
  56. *p = (size & 0x1F) << 3;
  57. memcpy(s->buf_ptr, buff, size);
  58. s->buf_ptr += size;
  59. } else {
  60. if (s->buf_ptr != s->buf + MAX_AU_HEADERS_SIZE) {
  61. av_log(s1, AV_LOG_ERROR, "Strange...\n");
  62. av_abort();
  63. }
  64. max_packet_size = s->max_payload_size - 4;
  65. p = s->buf;
  66. p[0] = 0;
  67. p[1] = 16;
  68. while (size > 0) {
  69. len = FFMIN(size, max_packet_size);
  70. p[2] = len >> 5;
  71. p[3] = (size & 0x1F) << 3;
  72. memcpy(p + 4, buff, len);
  73. ff_rtp_send_data(s1, p, len + 4, len == size);
  74. size -= len;
  75. buff += len;
  76. }
  77. }
  78. }