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.

56 lines
2.1 KiB

  1. From: Kurt Roeckx <kurt@roeckx.be>
  2. Date: Sun, 28 Jan 2018 15:44:08 +0100
  3. Subject: Check the size of the main data
  4. The main data to decode a frame can come from the current frame and part of the
  5. previous frame, the so called bit reservoir. si.main_data_begin is the part of
  6. the previous frame we need for this frame. frame_space is the amount of main
  7. data that can be in this frame, and next_md_begin is the part of this frame that
  8. is going to be used for the next frame.
  9. The maximum amount of data from a previous frame that the format allows is 511
  10. bytes. The maximum frame size for the defined bitrates is at MPEG 2.5 layer 2
  11. at 320 kbit/s and 8 kHz sample rate which gives 72 * (320000 / 8000) + 1 = 2881.
  12. So those defines are not large enough:
  13. # define MAD_BUFFER_GUARD 8
  14. # define MAD_BUFFER_MDLEN (511 + 2048 + MAD_BUFFER_GUARD)
  15. There is also support for a "free" bitrate which allows you to create any frame
  16. size, which can be larger than the buffer.
  17. Changing the defines is not an option since it's part of the ABI, so we check
  18. that the main data fits in the bufer.
  19. The previous frame data is stored in *stream->main_data and contains
  20. stream->md_len bytes. If stream->md_len is larger than the data we
  21. need from the previous frame (si.main_data_begin) it still wouldn't fit
  22. in the buffer, so just keep the data that we need.
  23. --- a/layer3.c
  24. +++ b/layer3.c
  25. @@ -2688,6 +2688,11 @@ int mad_layer_III(struct mad_stream *str
  26. next_md_begin = 0;
  27. md_len = si.main_data_begin + frame_space - next_md_begin;
  28. + if (md_len + MAD_BUFFER_GUARD > MAD_BUFFER_MDLEN) {
  29. + stream->error = MAD_ERROR_LOSTSYNC;
  30. + stream->sync = 0;
  31. + return -1;
  32. + }
  33. frame_used = 0;
  34. @@ -2705,8 +2710,11 @@ int mad_layer_III(struct mad_stream *str
  35. }
  36. }
  37. else {
  38. - mad_bit_init(&ptr,
  39. - *stream->main_data + stream->md_len - si.main_data_begin);
  40. + memmove(stream->main_data,
  41. + *stream->main_data + stream->md_len - si.main_data_begin,
  42. + si.main_data_begin);
  43. + stream->md_len = si.main_data_begin;
  44. + mad_bit_init(&ptr, *stream->main_data);
  45. if (md_len > si.main_data_begin) {
  46. assert(stream->md_len + md_len -