rtpenc_aac.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. void ff_rtp_send_aac(AVFormatContext *s1, const uint8_t *buff, int size)
  23. {
  24. RTPMuxContext *s = s1->priv_data;
  25. int len, max_packet_size;
  26. uint8_t *p;
  27. const int max_frames_per_packet = s->max_frames_per_packet ? s->max_frames_per_packet : 5;
  28. const int max_au_headers_size = 2 + 2 * max_frames_per_packet;
  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) > s->max_payload_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. int au_size = size;
  61. max_packet_size = s->max_payload_size - 4;
  62. p = s->buf;
  63. p[0] = 0;
  64. p[1] = 16;
  65. while (size > 0) {
  66. len = FFMIN(size, max_packet_size);
  67. p[2] = au_size >> 5;
  68. p[3] = (au_size & 0x1F) << 3;
  69. memcpy(p + 4, buff, len);
  70. ff_rtp_send_data(s1, p, len + 4, len == size);
  71. size -= len;
  72. buff += len;
  73. }
  74. }
  75. }