1 /*
2  * Copyright (C) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "audio_muxer_demo.h"
17 #include <iostream>
18 #include <cstdio>
19 #include <unistd.h>
20 #include <dlfcn.h>
21 #include <sys/stat.h>
22 
23 #include "avcodec_common.h"
24 #include "avcodec_errors.h"
25 #include "media_description.h"
26 #include "native_avformat.h"
27 #include "demo_log.h"
28 #include "avcodec_codec_name.h"
29 #include "native_avbuffer.h"
30 #include "ffmpeg_converter.h"
31 
32 using namespace OHOS;
33 using namespace OHOS::MediaAVCodec;
34 using namespace OHOS::MediaAVCodec::AudioBufferDemo;
35 
36 constexpr int32_t WIDTH_720 = 720;
37 constexpr int32_t HEIGHT_480 = 480;
38 constexpr int32_t WIDTH_3840 = 3840;
39 constexpr int32_t HEIGHT_2160 = 2160;
40 constexpr int32_t COLOR_TRANSFER_2 = 2;
41 constexpr int32_t COLOR_TRANSFER_18 = 18;
42 constexpr int32_t FRAME_RATE_30 = 30;
43 constexpr int32_t FRAME_RATE_60 = 60;
44 constexpr int32_t FRAME_SIZE_1024 = 1024;
45 constexpr int32_t COLOR_PRIMARIES_2 = 2;
46 constexpr int32_t COLOR_PRIMARIES_9 = 9;
47 constexpr int32_t COLOR_MATRIXCIEFF_2 = 2;
48 constexpr int32_t COLOR_MATRIXCIEFF_9 = 9;
49 constexpr int32_t VIDEO_DELAY_2 = 2;
50 
51 constexpr int32_t CHANNELS_2 = 2;
52 
53 constexpr int32_t INFO_PTS_100 = 100;
54 constexpr int32_t INFO_SIZE_1024 = 1024;
55 
56 constexpr int32_t SAMPLE_RATE_8000 = 8000;
57 constexpr int32_t SAMPLE_RATE_16000 = 16000;
58 constexpr int32_t SAMPLE_RATE_44100 = 44100;
59 
60 constexpr const char* MP4_OUTPUT_FILE_PATH = "/data/test/media/muxer_MP4_outputfile.mp4";
61 constexpr const char* AMRNB_OUTPUT_FILE_PATH = "/data/test/media/muxer_AMRNB_outputfile.amr";
62 constexpr const char* AMRWB_OUTPUT_FILE_PATH = "/data/test/media/muxer_AMRWB_outputfile.amr";
63 constexpr const char* M4A_OUTPUT_FILE_PATH = "/data/test/media/muxer_M4A_outputfile.m4a";
64 constexpr const char* MP3_OUTPUT_FILE_PATH = "/data/test/media/muxer_MP3_outputfile.mp3";
65 
AVMuxerDemo()66 AVMuxerDemo::AVMuxerDemo()
67 {
68 }
~AVMuxerDemo()69 AVMuxerDemo::~AVMuxerDemo()
70 {
71     if (avmuxer_) {
72         OH_AVMuxer_Destroy(avmuxer_);
73         avmuxer_ = nullptr;
74     }
75     if (avbuffer) {
76         OH_AVBuffer_Destroy(avbuffer);
77         avbuffer = nullptr;
78     }
79     if (outputFd_ > -1) {
80         close(outputFd_);
81         outputFd_ = -1;
82     }
83 }
84 
InitFile(const std::string & inputFile)85 bool AVMuxerDemo::InitFile(const std::string& inputFile)
86 {
87     if (inputFile.find("aac") != std::string::npos) {
88         audioType_ = AudioMuxerFormatType::TYPE_AAC;
89         output_path_ = MP4_OUTPUT_FILE_PATH;
90         output_format_ = OH_AVOutputFormat::AV_OUTPUT_FORMAT_M4A;
91     } else if (inputFile.find("h264") != std::string::npos) {
92         audioType_ = AudioMuxerFormatType::TYPE_H264;
93         output_format_ = OH_AVOutputFormat::AV_OUTPUT_FORMAT_MPEG_4;
94     } else if (inputFile.find("h265") != std::string::npos) {
95         audioType_ = AudioMuxerFormatType::TYPE_H265;
96         output_format_ = OH_AVOutputFormat::AV_OUTPUT_FORMAT_MPEG_4;
97     } else if (inputFile.find("mpeg4") != std::string::npos) {
98         audioType_ = AudioMuxerFormatType::TYPE_MPEG4;
99         output_format_ = OH_AVOutputFormat::AV_OUTPUT_FORMAT_MPEG_4;
100     } else if (inputFile.find("hdr") != std::string::npos) {
101         audioType_ = AudioMuxerFormatType::TYPE_HDR;
102         output_format_ = OH_AVOutputFormat::AV_OUTPUT_FORMAT_MPEG_4;
103     } else if (inputFile.find("jpg") != std::string::npos) {
104         audioType_ = AudioMuxerFormatType::TYPE_JPG;
105         output_path_ = M4A_OUTPUT_FILE_PATH;
106         output_format_ = OH_AVOutputFormat::AV_OUTPUT_FORMAT_MPEG_4;
107     } else if (inputFile.find("amrwb") != std::string::npos) {
108         audioType_ = AudioMuxerFormatType::TYPE_AMRWB;
109         output_path_ = AMRWB_OUTPUT_FILE_PATH;
110         output_format_ = OH_AVOutputFormat::AV_OUTPUT_FORMAT_AMR;
111     } else if (inputFile.find("amrnb") != std::string::npos) {
112         audioType_ = AudioMuxerFormatType::TYPE_AMRNB;
113         output_path_ = AMRNB_OUTPUT_FILE_PATH;
114         output_format_ = OH_AVOutputFormat::AV_OUTPUT_FORMAT_AMR;
115     } else if (inputFile.find("mpeg3") != std::string::npos) {
116         audioType_ = AudioMuxerFormatType::TYPE_MPEG3;
117         output_path_ = MP3_OUTPUT_FILE_PATH;
118         output_format_ = OH_AVOutputFormat::AV_OUTPUT_FORMAT_MP3;
119     }
120     return true;
121 }
RunCase(const uint8_t * data,size_t size)122 bool AVMuxerDemo::RunCase(const uint8_t *data, size_t size)
123 {
124     std::string codecdata(reinterpret_cast<const char *>(data), size);
125     inputdata = codecdata;
126     inputdatasize = size;
127     std::string outputFile(output_path_);
128 	//Create
129     avmuxer_ = Create();
130     DEMO_CHECK_AND_RETURN_RET_LOG(avmuxer_ != nullptr, false, "Fatal: Create fail");
131     DEMO_CHECK_AND_RETURN_RET_LOG(SetRotation(avmuxer_, 0) == AVCS_ERR_OK, false, "Fatal: SetRotation fail");
132     //获取param
133     AudioTrackParam param = InitFormatParam(audioType_);
134     int32_t trackIndex = -1;
135     //添加轨
136     int32_t res = 0;
137     if (param.isNeedCover) {
138         res = AddCoverTrack(avmuxer_, trackIndex, param);
139         DEMO_CHECK_AND_RETURN_RET_LOG(res == AVCS_ERR_OK, false, "Fatal: AddTrack fail");
140     } else {
141         res = AddTrack(avmuxer_, trackIndex, param);
142         DEMO_CHECK_AND_RETURN_RET_LOG(res == AVCS_ERR_OK, false, "Fatal: AddTrack fail");
143     }
144     //Start
145     DEMO_CHECK_AND_RETURN_RET_LOG(Start() == AVCS_ERR_OK, false, "Fatal: Start fail");
146     //写数据
147     if (param.isNeedCover) {
148         WriteTrackCover(avmuxer_, trackIndex);
149     } else {
150         WriteSingleTrackSampleAVBuffer(avmuxer_, trackIndex);
151     }
152     //Stop
153     DEMO_CHECK_AND_RETURN_RET_LOG(Stop() == AVCS_ERR_OK, false, "Fatal: Stop fail");
154     //Destroy
155     DEMO_CHECK_AND_RETURN_RET_LOG(Destroy() == AVCS_ERR_OK, false, "Fatal: Destroy fail");
156     return true;
157 }
158 
InitFormatParam(AudioMuxerFormatType type)159 AudioTrackParam AVMuxerDemo::InitFormatParam(AudioMuxerFormatType type)
160 {
161     AudioTrackParam param;
162     param.sampleRate = SAMPLE_RATE_44100;
163     param.channels = CHANNELS_2;
164     param.frameSize = FRAME_SIZE_1024;
165     param.width = WIDTH_720;
166     param.height = HEIGHT_480;
167     param.isNeedCover = false;
168     param.frameRate = FRAME_RATE_60;
169     param.videoDelay = 0;
170     param.colorPrimaries = COLOR_PRIMARIES_2;
171     param.colorTransfer = COLOR_TRANSFER_2;
172     param.colorMatrixCoeff = COLOR_MATRIXCIEFF_2;
173     param.colorRange = 0;
174     param.isHdrVivid = 0;
175     param.isNeedCover = false;
176     param.isNeedVideo = false;
177     if (audioType_ == AudioMuxerFormatType::TYPE_AAC) {
178         param.mimeType = OH_AVCODEC_MIMETYPE_AUDIO_AAC;
179     } else if (audioType_ == AudioMuxerFormatType::TYPE_H264) {
180         param.mimeType = OH_AVCODEC_MIMETYPE_VIDEO_AVC;
181     } else if (audioType_ == AudioMuxerFormatType::TYPE_H265) {
182         param.mimeType = OH_AVCODEC_MIMETYPE_VIDEO_HEVC;
183         param.videoDelay = VIDEO_DELAY_2;
184     } else if (audioType_ == AudioMuxerFormatType::TYPE_MPEG3 || audioType_ == AudioMuxerFormatType::TYPE_MPEG4) {
185         param.mimeType = OH_AVCODEC_MIMETYPE_AUDIO_MPEG;
186     } else if (audioType_ == AudioMuxerFormatType::TYPE_HDR) {
187         param.mimeType = OH_AVCODEC_MIMETYPE_VIDEO_HEVC;
188         param.width = WIDTH_3840;
189         param.height = HEIGHT_2160;
190         param.frameRate = FRAME_RATE_30;
191         param.colorPrimaries = COLOR_PRIMARIES_9;
192         param.colorTransfer = COLOR_TRANSFER_18;
193         param.colorMatrixCoeff = COLOR_MATRIXCIEFF_9;
194         param.isHdrVivid = 1;
195     } else if (audioType_ == AudioMuxerFormatType::TYPE_JPG) {
196         param.mimeType = OH_AVCODEC_MIMETYPE_IMAGE_JPG;
197         param.isNeedCover = true;
198     } else if (audioType_ == AudioMuxerFormatType::TYPE_AMRNB) {
199         param.mimeType = OH_AVCODEC_MIMETYPE_AUDIO_AMR_NB;
200         param.sampleRate = SAMPLE_RATE_8000;
201         param.channels = 1;
202     } else if (audioType_ == AudioMuxerFormatType::TYPE_AMRWB) {
203         param.mimeType = OH_AVCODEC_MIMETYPE_AUDIO_AMR_WB;
204         param.sampleRate = SAMPLE_RATE_16000;
205         param.channels = 1;
206     }
207     return param;
208 }
209 
Create()210 OH_AVMuxer* AVMuxerDemo::Create()
211 {
212     std::string outputFile(output_path_);
213     outputFd_ = open(outputFile.c_str(), O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR);
214     if (outputFd_ < 0) {
215         std::cout << "open file failed! outputFd_ is:" << outputFd_ << std::endl;
216         return nullptr;
217     }
218     return OH_AVMuxer_Create(outputFd_, output_format_);
219 }
220 
Start()221 int32_t AVMuxerDemo::Start()
222 {
223     return OH_AVMuxer_Start(avmuxer_);
224 }
225 
Stop()226 int32_t AVMuxerDemo::Stop()
227 {
228     return OH_AVMuxer_Stop(avmuxer_);
229 }
230 
Destroy()231 int32_t AVMuxerDemo::Destroy()
232 {
233     if (avbuffer != nullptr) {
234         OH_AVBuffer_Destroy(avbuffer);
235         avbuffer = nullptr;
236     }
237     int32_t res = 0;
238     if (avmuxer_ != nullptr) {
239         res = OH_AVMuxer_Destroy(avmuxer_);
240         avmuxer_ = nullptr;
241     }
242     return res;
243 }
244 
SetRotation(OH_AVMuxer * muxer,int32_t rotation)245 int32_t AVMuxerDemo::SetRotation(OH_AVMuxer* muxer, int32_t rotation)
246 {
247     return OH_AVMuxer_SetRotation(muxer, rotation);
248 }
249 
AddTrack(OH_AVMuxer * muxer,int32_t & trackIndex,AudioTrackParam param)250 int32_t AVMuxerDemo::AddTrack(OH_AVMuxer* muxer, int32_t& trackIndex, AudioTrackParam param)
251 {
252     const int configBufferSize =  0x1FFF;
253     OH_AVFormat *trackFormat = OH_AVFormat_Create();
254     if (trackFormat == NULL) {
255         std::cout << "AddTrack: format failed!" << std::endl;
256         return AV_ERR_INVALID_VAL;
257     }
258     // set codec config
259     int extraSize = 0;
260     unsigned char buffer[configBufferSize] = {0};
261     if (extraSize <= configBufferSize && extraSize > 0) {
262         errno_t res = 0;
263         res = strncpy_s(reinterpret_cast<char*>(buffer), extraSize, inputdata.c_str(), extraSize);
264         if (res != 0) {
265             return res;
266         }
267         OH_AVFormat_SetBuffer(trackFormat, OH_MD_KEY_CODEC_CONFIG, buffer, extraSize);
268     }
269     OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, param.mimeType);
270     // audio
271     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, param.sampleRate);
272     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, param.channels);
273     OH_AVFormat_SetIntValue(trackFormat, "audio_samples_per_frame", param.frameSize);
274     //video
275     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_WIDTH, param.width);
276     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_HEIGHT, param.height);
277     OH_AVFormat_SetDoubleValue(trackFormat, OH_MD_KEY_FRAME_RATE, param.frameRate);
278     OH_AVFormat_SetIntValue(trackFormat, "video_delay", param.videoDelay);
279     // hdr vivid
280     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_COLOR_PRIMARIES, param.colorPrimaries);
281     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_TRANSFER_CHARACTERISTICS, param.colorTransfer);
282     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_MATRIX_COEFFICIENTS, param.colorMatrixCoeff);
283     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_RANGE_FLAG, param.colorRange);
284     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_VIDEO_IS_HDR_VIVID, param.isHdrVivid);
285     int32_t ret = OH_AVMuxer_AddTrack(muxer, &trackIndex, trackFormat);
286     return ret;
287 }
288 
289 
AddCoverTrack(OH_AVMuxer * muxer,int32_t & trackId,AudioTrackParam param)290 int32_t AVMuxerDemo::AddCoverTrack(OH_AVMuxer* muxer, int32_t& trackId, AudioTrackParam param)
291 {
292     OH_AVFormat *trackFormat = OH_AVFormat_Create();
293     if (trackFormat == NULL) {
294         std::cout << "format failed!" << std::endl;
295         return AV_ERR_INVALID_VAL;
296     }
297     OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, param.mimeType);
298     // audio
299     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, param.sampleRate);
300     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, param.channels);
301     OH_AVFormat_SetIntValue(trackFormat, "audio_samples_per_frame", param.frameSize);
302     //video
303     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_WIDTH, param.width);
304     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_HEIGHT, param.height);
305     OH_AVFormat_SetDoubleValue(trackFormat, OH_MD_KEY_FRAME_RATE, param.frameRate);
306     OH_AVFormat_SetIntValue(trackFormat, "video_delay", param.videoDelay);
307     // hdr vivid
308     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_COLOR_PRIMARIES, param.colorPrimaries);
309     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_TRANSFER_CHARACTERISTICS, param.colorTransfer);
310     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_MATRIX_COEFFICIENTS, param.colorMatrixCoeff);
311     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_RANGE_FLAG, param.colorRange);
312     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_VIDEO_IS_HDR_VIVID, param.isHdrVivid);
313     int32_t ret = OH_AVMuxer_AddTrack(muxer, &trackId, trackFormat);
314     return ret;
315 }
316 
WriteTrackCover(OH_AVMuxer * muxer,int32_t trackIndex)317 void AVMuxerDemo::WriteTrackCover(OH_AVMuxer *muxer, int32_t trackIndex)
318 {
319     OH_AVCodecBufferAttr info;
320     memset_s(&info, sizeof(info), 0, sizeof(info));
321     info.size = inputdatasize;
322     OH_AVBuffer *av_buffer = OH_AVBuffer_Create(info.size);
323     if (av_buffer == NULL) {
324         std::cout << "WriteTrackCover: create OH_AVMemory error!" << std::endl;
325         return;
326     }
327     size_t frameBytes = 1024;
328     size_t cSize = inputdatasize < frameBytes ? inputdatasize : frameBytes;
329     errno_t res = 0;
330     res = strncpy_s(reinterpret_cast<char*>(OH_AVBuffer_GetAddr(av_buffer)), cSize, inputdata.c_str(), cSize);
331     if (res != 0) {
332         return;
333     }
334     if (OH_AVMuxer_WriteSampleBuffer(muxer, trackIndex, av_buffer) != AV_ERR_OK) {
335         OH_AVBuffer_Destroy(av_buffer);
336         av_buffer = nullptr;
337         std::cout << "OH_AVMuxer_WriteSample error!" << std::endl;
338         return;
339     }
340     if (av_buffer != nullptr) {
341         OH_AVBuffer_Destroy(av_buffer);
342         av_buffer = nullptr;
343     }
344 }
345 
WriteSingleTrackSampleAVBuffer(OH_AVMuxer * muxer,int32_t trackIndex)346 void AVMuxerDemo::WriteSingleTrackSampleAVBuffer(OH_AVMuxer *muxer, int32_t trackIndex)
347 {
348     if (muxer == NULL || trackIndex  < 0) {
349         std::cout << "WriteSingleTrackSample muxer is null " << trackIndex << std::endl;
350         return;
351     }
352     OH_AVCodecBufferAttr info;
353     OH_AVBuffer *buffer = NULL;
354     memset_s(&info, sizeof(info), 0, sizeof(info));
355     bool ret = UpdateWriteBufferInfoAVBuffer(&buffer, &info);
356 	//此处只执行一次,正常要根据内容进行while循环将buffer所有数据写入,因为fuzz数据是假数据,故仅调用一次;
357     if (ret) {
358         OH_AVMuxer_WriteSampleBuffer(muxer, trackIndex, buffer);
359     }
360     if (buffer != NULL) {
361         OH_AVBuffer_Destroy(buffer);
362         buffer = nullptr;
363     }
364 }
365 
UpdateWriteBufferInfoAVBuffer(OH_AVBuffer ** buffer,OH_AVCodecBufferAttr * info)366 bool AVMuxerDemo::UpdateWriteBufferInfoAVBuffer(OH_AVBuffer **buffer, OH_AVCodecBufferAttr *info)
367 {
368     //此处不区分音频类型,所有pts、size、flags均取一样的值
369     info->pts = INFO_PTS_100;
370     info->size = INFO_SIZE_1024;
371     info->flags = AVCODEC_BUFFER_FLAGS_NONE;
372     if (buffer == NULL || info == NULL) {
373         return false;
374     }
375     if (*buffer != NULL) {
376         std::cout << "UpdateWriteBufferInfoAVBuffer: buffer is NULL!" << std::endl;
377         OH_AVBuffer_Destroy(*buffer);
378         *buffer = NULL;
379     }
380     if (*buffer == NULL) {
381         *buffer = OH_AVBuffer_Create(info->size);
382     }
383     if (*buffer == NULL) {
384         std::cout << "UpdateWriteBufferInfoAVBuffer: error create OH_AVMemory! " << std::endl;
385         return false;
386     }
387     errno_t res = 0;
388     res = strncpy_s(reinterpret_cast<char *>(OH_AVBuffer_GetAddr(*buffer)), info->size, inputdata.c_str(), info->size);
389     if (res != 0) {
390         return false;
391     }
392     OH_AVBuffer_SetBufferAttr(*buffer, info);
393     return true;
394 }