Multiplex Codec Bug Fix: Padding Needed For H264

This CL fixes the bug that we must have some padding for H264 codec
when used in Multiplex encoder/decoder, otherwise H264 decoder will
crash.

And this CL fixes a bug that potential infinite loop exists in
MultiplexEncoderAdapter

Bug: webrtc:8921
Change-Id: I4124579c31ee69f72e29d118378aa1f8b3f05eb4
Reviewed-on: https://webrtc-review.googlesource.com/56960
Commit-Queue: Qiang Chen <qiangchen@chromium.org>
Reviewed-by: Emircan Uysaler <emircan@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22175}
diff --git a/modules/video_coding/codecs/multiplex/multiplex_encoded_image_packer.cc b/modules/video_coding/codecs/multiplex/multiplex_encoded_image_packer.cc
index 83be3a7..33f9c4f 100644
--- a/modules/video_coding/codecs/multiplex/multiplex_encoded_image_packer.cc
+++ b/modules/video_coding/codecs/multiplex/multiplex_encoded_image_packer.cc
@@ -139,8 +139,10 @@
     frame_header.component_index = images[i].component_index;
 
     frame_header.bitstream_offset = bitstream_offset;
+    const size_t padding =
+        EncodedImage::GetBufferPaddingBytes(images[i].codec_type);
     frame_header.bitstream_length =
-        static_cast<uint32_t>(images[i].encoded_image._length);
+        static_cast<uint32_t>(images[i].encoded_image._length + padding);
     bitstream_offset += frame_header.bitstream_length;
 
     frame_header.codec_type = images[i].codec_type;
@@ -213,8 +215,11 @@
     EncodedImage encoded_image = combined_image;
     encoded_image._timeStamp = combined_image._timeStamp;
     encoded_image._frameType = frame_headers[i].frame_type;
-    encoded_image._length = encoded_image._size =
+    encoded_image._size =
         static_cast<size_t>(frame_headers[i].bitstream_length);
+    const size_t padding =
+        EncodedImage::GetBufferPaddingBytes(image_component.codec_type);
+    encoded_image._length = encoded_image._size - padding;
     encoded_image._buffer =
         combined_image._buffer + frame_headers[i].bitstream_offset;
 
diff --git a/modules/video_coding/codecs/multiplex/multiplex_encoder_adapter.cc b/modules/video_coding/codecs/multiplex/multiplex_encoder_adapter.cc
index 2754b39..0d797ec 100644
--- a/modules/video_coding/codecs/multiplex/multiplex_encoder_adapter.cc
+++ b/modules/video_coding/codecs/multiplex/multiplex_encoder_adapter.cc
@@ -234,8 +234,9 @@
 
   if (stashed_image.image_components.size() == frame_count) {
     // Complete case
-    auto iter = stashed_images_.begin();
-    while (iter != stashed_images_.end() && iter != stashed_image_next_itr) {
+    for (auto iter = stashed_images_.begin();
+         iter != stashed_images_.end() && iter != stashed_image_next_itr;
+         iter++) {
       // No image at all, skip.
       if (iter->second.image_components.size() == 0)
         continue;
@@ -252,7 +253,6 @@
       codec_info.codecSpecific.generic.simulcast_idx = 0;
       encoded_complete_callback_->OnEncodedImage(combined_image_, &codec_info,
                                                  fragmentation);
-      iter++;
     }
 
     stashed_images_.erase(stashed_images_.begin(), stashed_image_next_itr);