QGAnimatedImageBufferManager.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // QGAnimatedImageBufferManager.m
  2. // Tencent is pleased to support the open source community by making vap available.
  3. //
  4. // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  5. //
  6. // Licensed under the MIT License (the "License"); you may not use this file except in
  7. // compliance with the License. You may obtain a copy of the License at
  8. //
  9. // http://opensource.org/licenses/MIT
  10. //
  11. // Unless required by applicable law or agreed to in writing, software distributed under the License is
  12. // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  13. // either express or implied. See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #import "QGAnimatedImageBufferManager.h"
  16. #import "QGVAPSafeMutableArray.h"
  17. @interface QGAnimatedImageBufferManager() {
  18. QGAnimatedImageDecodeConfig *_config; //解码配置
  19. }
  20. @end
  21. @implementation QGAnimatedImageBufferManager
  22. - (instancetype)initWithConfig:(QGAnimatedImageDecodeConfig *)config {
  23. if (self = [super init]) {
  24. _config = config;
  25. [self createBuffersWithConfig:config];
  26. }
  27. return self;
  28. }
  29. - (void)createBuffersWithConfig:(QGAnimatedImageDecodeConfig *)config {
  30. _buffers = [[QGVAPSafeMutableArray alloc] initWithCapacity:config.bufferCount];
  31. }
  32. /**
  33. 取出指定的在缓冲区的帧,若不存在于缓冲区则返回空
  34. @param frameIndex 目标帧索引
  35. @return 帧数据
  36. */
  37. - (QGBaseAnimatedImageFrame *)getBufferedFrame:(NSInteger)frameIndex {
  38. if (_buffers.count == 0) {
  39. //MOLogV(@"fail buffer is nil");
  40. return nil;
  41. }
  42. NSInteger bufferIndex = frameIndex%_buffers.count;
  43. if (bufferIndex > _buffers.count-1) {
  44. //MOLogV(@"fail");
  45. return nil;
  46. }
  47. id frame = [_buffers objectAtIndex:bufferIndex];
  48. if (![frame isKindOfClass:[QGBaseAnimatedImageFrame class]] || ([(QGBaseAnimatedImageFrame*)frame frameIndex] != frameIndex)) {
  49. return nil;
  50. }
  51. return frame;
  52. }
  53. - (QGBaseAnimatedImageFrame *)popVideoFrame {
  54. if (!_buffers.count) {
  55. return nil;
  56. }
  57. if (![_buffers.firstObject isKindOfClass:[QGBaseAnimatedImageFrame class]]) {
  58. return nil;
  59. }
  60. QGBaseAnimatedImageFrame *frame = _buffers.firstObject;
  61. [_buffers removeObjectAtIndex:0];
  62. return frame;
  63. }
  64. /**
  65. 判断当前缓冲区是否被填满
  66. @return 只有当缓冲区所有区域都被QGBaseAnimatedImageFrame类型的数据填满才算缓冲区满
  67. */
  68. - (BOOL)isBufferFull {
  69. __block BOOL isFull = YES;
  70. [_buffers enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  71. if (![obj isKindOfClass:[QGBaseAnimatedImageFrame class]]) {
  72. isFull = NO;
  73. *stop = YES;
  74. }
  75. }];
  76. return isFull;
  77. }
  78. - (void)dealloc {
  79. }
  80. @end