Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 1개 있습니다.)
(시리즈 글이 8개 있습니다.)
.NET Framework: 1140. C# - ffmpeg(FFmpeg.AutoGen)를 이용해 MP3 오디오 파일 인코딩/디코딩하는 예제
; https://www.sysnet.pe.kr/2/0/12939

.NET Framework: 1144. C# - ffmpeg(FFmpeg.AutoGen) AVFormatContext를 이용해 ffprobe처럼 정보 출력
; https://www.sysnet.pe.kr/2/0/12948

.NET Framework: 1145. C# - ffmpeg(FFmpeg.AutoGen) - Codec 정보 열람 및 사용 준비
; https://www.sysnet.pe.kr/2/0/12949

.NET Framework: 1148.  C# - ffmpeg(FFmpeg.AutoGen) - decoding 과정
; https://www.sysnet.pe.kr/2/0/12956

.NET Framework: 1149. C# - ffmpeg(FFmpeg.AutoGen) - 비디오 프레임 디코딩
; https://www.sysnet.pe.kr/2/0/12958

.NET Framework: 1155. C# - ffmpeg(FFmpeg.AutoGen): Bitmap으로부터 yuv420p + rawvideo 형식의 파일로 쓰기
; https://www.sysnet.pe.kr/2/0/12966

.NET Framework: 1156. C# - ffmpeg(FFmpeg.AutoGen): Bitmap으로부터 h264 형식의 파일로 쓰기
; https://www.sysnet.pe.kr/2/0/12970

.NET Framework: 1160. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 qsv 디코딩
; https://www.sysnet.pe.kr/2/0/12977




C# - ffmpeg(FFmpeg.AutoGen)를 이용해 MP3 오디오 파일 인코딩/디코딩하는 예제

지난 글에서,

C# - ffmpeg(FFmpeg.AutoGen)를 이용해 MP2 오디오 파일 디코딩 예제(decode_audio.c)
; https://www.sysnet.pe.kr/2/0/12933

C# - ffmpeg(FFmpeg.AutoGen)를 이용해 오디오(mp2) 인코딩하는 예제(encode_audio.c)
; https://www.sysnet.pe.kr/2/0/12937

MP2 파일을 인코딩/디코딩했는데요, 그렇다면 MP3 파일은 어떨까요? 일단 인코딩 먼저 볼까요? ^^

지난 소스 코드를 참고해, 단순히 인코더만 MP3로 선택하면,

codec = ffmpeg.avcodec_find_encoder(AVCodecID.AV_CODEC_ID_MP3);

이후 ffmpeg.av_frame_get_buffer 호출에서 -22를 반환합니다. (소스 코드 내에서의 오류 메시지: "Could not allocate audio data buffers")

[mp3_mf @ 00000288337D7B00] MFT name: 'MP3 Encoder ACM Wrapper MFT'
Could not allocate audio data buffers

함께 출력된 로그를 보면 "MP3 Encoder ACM Wrapper MFT"라고 나오는데, 검색해 보면 MFT는 Media Foundation Transform의 약자로, 아마도 마이크로소프트의 운영체제에 포함된 내장 인코더로 보입니다.

MP3 Audio Encoder
; https://docs.microsoft.com/en-us/windows/win32/medfound/mp3-audio-encoder

음... 저 인코더는 뭔가 특별한 게 있는 걸까요? ^^;




혹시나 싶어 AV_CODEC_ID_MP3 검색어로 찾아봤더니,

ffmpeg-toys/avcodec.c
; https://github.com/joelanders/ffmpeg-toys/blob/master/avcodec.c

예제가 나옵니다. 하지만 이 예제 코드는 av_frame_get_buffer 호출까지 가기도 전에 이미 check_sample_fmt에서 걸립니다.

c->sample_fmt = AVSampleFormat.AV_SAMPLE_FMT_S16P;
if (check_sample_fmt(codec, c->sample_fmt) == 0)
{
    Console.WriteLine($"Encoder does not support sample format {ffmpeg.av_get_sample_fmt_name(c->sample_fmt)}");
    break;
}

check_sample_fmt 함수의 내부를 디버깅해 보면 ("MP3 Encoder ACM Wrapper MFT" 코덱이) AV_SAMPLE_FMT_S16만 지원한다는 것을 알 수 있습니다. (어쨌든, 저 코드를 작성한 개발자의 PC에서는 잘 동작했을 것입니다.)

게다가 그 외의 "ffmpeg-toys/avcodec.c" 코드가 이전 mp2 인코딩과 다른 걸로 봐서는 아마도 deprecated 함수로 구성이 된 게 아닌가 싶습니다.




좀 뚝딱거리다 보니, 해당 코덱의 경우 avcodec_open2 호출 이후에 c->frame_size가 0이라는 점이 독특합니다. 왜냐하면, MP2 예제에서는 1152 값이 나왔기 때문입니다.

아마도 이것 때문에 이후 av_samples_get_buffer_size, av_frame_get_buffer 등의 호출에서 반환값이 -22가 나온 듯합니다.

ffmpeg.AVERROR(ffmpeg.EAGAIN); // -11
ffmpeg.AVERROR_EOF; // -541478725
ffmpeg.AVERROR(ffmpeg.EINVAL); // -22
ffmpeg.AVERROR(ffmpeg.ENOMEM); // -12
ffmpeg.AVERROR(ffmpeg.EPIPE); // -32

실제로 av_samples_get_buffer_size의 소스 코드를 보면,

// https://ffmpeg.org/doxygen/trunk/samplefmt_8c_source.html#l00119
 int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples,
                                enum AVSampleFormat sample_fmt, int align)
 {
     int line_size;
     int sample_size = av_get_bytes_per_sample(sample_fmt);
     int planar      = av_sample_fmt_is_planar(sample_fmt);
  
     /* validate parameter ranges */
     if (!sample_size || nb_samples <= 0 || nb_channels <= 0)
         return AVERROR(EINVAL);
  
     ...[생략]...
 }

(frame_size가 담긴) nb_samples가 0인 경우 AVERROR(EINVAL)을 반환합니다. 그리고 av_frame_get_buffer의 경우에도 frame->nb_samples의 값이 0보다 크지 않으면 AVERROR(EINVAL)을 반환합니다.

// https://ffmpeg.org/doxygen/trunk/frame_8c_source.html#l00243
 int av_frame_get_buffer(AVFrame *frame, int align)
 {
     if (frame->format < 0)
         return AVERROR(EINVAL);
  
     if (frame->width > 0 && frame->height > 0)
         return get_video_buffer(frame, align);
     else if (frame->nb_samples > 0 && (frame->channel_layout || frame->channels > 0))
         return get_audio_buffer(frame, align);
  
     return AVERROR(EINVAL);
 }

이와 관련해 Q&A에 보면,

ffmpeg audio encoder frame size
; https://stackoverflow.com/questions/60997236/ffmpeg-audio-encoder-frame-size

코덱에 AV_CODEC_CAP_VARIABLE_FRAME_SIZE가 설정돼 있으면 avcodec_open2 호출에도 frame_size가 0으로 나온다고 합니다.

그런데, 제 경우에는 확인을 해봐도,

if ((codec->capabilities & ffmpeg.AV_CODEC_CAP_VARIABLE_FRAME_SIZE) == ffmpeg.AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
{
    Console.WriteLine("Codec: ffmpeg.AV_CODEC_CAP_VARIABLE_FRAME_SIZE");
}

저 조건이 걸리지 않습니다. 왜냐하면, codec->capabilities 값이 0x00080022이고, AV_CODEC_CAP_VARIABLE_FRAME_SIZE는 0x00010000이기 때문입니다.




밑져야 본전이라는 생각으로 ^^ 일부러 (mp2에서 구했던) 1152를 c->frame_size에 대입해 봤습니다.

if (c->frame_size == 0)
{
    c->frame_size = 1152;
}

frame->nb_samples = c->frame_size;
frame->format = (int)c->sample_fmt;
frame->channel_layout = c->channel_layout;

/* allocate the data buffers */
ret = ffmpeg.av_frame_get_buffer(frame, 0);
if (ret < 0)
{
    Console.WriteLine("Could not allocate audio data buffers");
    break;
}

그랬더니 잘 동작합니다. ^^; 출력된 mp3 파일을 ffprobe로도 이렇게 확인할 수 있습니다.

C:\temp\output> ffprobe test.mp3
ffprobe version 4.4.1 Copyright (c) 2007-2021 the FFmpeg developers
  built with Microsoft (R) C/C++ Optimizing Compiler Version 19.30.30705 for x64
  configuration: ...[생략]... --extra-cxxflags=-MD
  libavutil      56. 70.100 / 56. 70.100
  ...[생략]...
  libpostproc    55.  9.100 / 55.  9.100
[mp3 @ 000001EA6C3B0380] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'test.mp3':
  Duration: 00:00:05.27, start: 0.000000, bitrate: 96 kb/s
  Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 96 kb/s

그런데 좀 이상하군요, 분명히 소스 코드에서는 "c->bit_rate = 64000;"라고 지정했는데 위의 출력에서는 96kb/s로 나옵니다. (MP2 인코딩 예제에서는 64kb/s로 나옵니다.) 이와 함께 Duration도 mp2 인코딩에서는 5.22가 나왔지만 위의 경우 5.27로 나옵니다.

한 가지 더 이상한 것은, MP2의 경우 "c = ffmpeg.avcodec_alloc_context3(codec);" 실행 시 c->bit_rate가 0으로 나오기 때문에 명시적으로 64000을 설정하는 것이 의미가 있지만, MP3의 경우에는 동일한 코드에서 c->bit_rate가 이미 128000으로 초기화가 되어 있습니다. 그걸 강제로 64000으로 설정하는 것이 의미가 없는 걸까요? (그렇다고 해도 상식적으로, 동일한 데이터에 bitrate가 높게 설정되면 시간이 줄어야 할 듯한데... ^^;)

참고로, 아래의 글을 보면,

BASIC4MCU | 하드웨어 | AUDIO | MPEG1 Audio Layer3 / mp3 파일구조
; https://www.basic4mcu.com/bbs/board.php?bo_table=gesiyo8&wr_id=480

"mp3는 프레임당 1152 비트가 할당되기 때문에 바이트 계산하면 144바이트가 된다."라는 설명이 나옵니다. 비디오의 경우에는 프레임이 하나의 영상 크기라는 것을 알겠지만, 오디오의 경우 frame_size가 명확히 다가오지 않습니다. 비디오 영상의 30fps라는 의미가 오디오에서는 보통 샘플링 주파수인 44100 Hz 등과 비교될 수 있을 것 같습니다. 그런 의미에서 어쩌면 오디오에서는 채널 수(스테레오 2채널)와 샘플링 비트(16비트 PCM)가 frame_size의 의미가 될 듯하지만, 1152와 32라는 숫자 간에 괴리감이 있습니다.

게다가 위의 설명에서 "프레임당 ... (크기)"에 대한 설명을 발췌하면 다음과 같이 서로 다른 의미를 갖는 동일한 설명이 됩니다.

mp3는 프레임당 1152 비트가 할당되기 때문에...[생략]... 프레임당 약 9558바이트의 Size가 된다.

음... 아무리 제가 이 분야로 초보라지만... ^^;




자, 어쨌든 우여곡절 끝에 인코딩을 성공했으니 그럼 지난번 예제에서 못했던 MP3 디코딩을 해볼까요? ^^

그 글에서도 언급했지만, 해당 예제를 AV_CODEC_ID_MP3 식별자로 바꿔 avcodec_find_decoder를 호출하면 이후 avcodec_send_packet에서 ret 값이 -1094995529로 오류를 반환합니다.

ret = ffmpeg.avcodec_send_packet(dec_ctx, packet);
if (ret < 0)
{
    Console.WriteLine($"Error submitting the packet to the decoder: {ret}");
    return false;
}

원래의 C 언어 예제에서는 위의 코드가 "return false"가 아닌 exit(1)로 되어 있기 때문에,

ret = avcodec_send_packet(dec_ctx, pkt);
if (ret < 0) {
    fprintf(stderr, "Error submitting the packet to the decoder\n");
    exit(1);
}

저도 C#으로 포팅하며 false를 반환한 경우 더 이상 디코딩 작업을 하지 않도록 변경한 것인데요. 재미있는 것은, 그냥 avcodec_send_packet에서 오류가 발생해도 그걸 무시하고 진행해도 된다는 점입니다. 즉, 그냥 true를 반환하도록 해서,

ret = ffmpeg.avcodec_send_packet(dec_ctx, packet);
if (ret < 0)
{
    Console.WriteLine($"Error submitting the packet to the decoder: {ret}");
    return true;
}

디코딩 작업을 계속 이어나가면 됩니다. 그럼 화면에는 다음과 같은 식으로 출력이 나오는데,

[mp3float @ 0000023F7FAD32C0] Header missing
Error submitting the packet to the decoder: -1094995529
[mp3float @ 0000023F7FAD32C0] Header missing
Error submitting the packet to the decoder: -1094995529
[mp3float @ 0000023F7FAD32C0] Header missing
Error submitting the packet to the decoder: -1094995529
Warning: the sample format the decoder produced is planar 0. This example will output the first channel only.
Play the output audio file with the command:
ffplay -f f32le -ac 2 -ar 44100 c:\temp\output\test_cs2.dat

3번의 "Header missing"이 나왔지만 그것에 상관없이 출력된 "test_cs2.dat" 파일은 정상적으로 ffplay로 재생이 됩니다.

대신 MP2 파일을 디코딩한 파일은 41,405KB 크기로 ffprobe의 결과가 adpcm_dtk였지만, MP3 파일의 경우에는 그 2배인 82,809KB에 ffprobe로는 결과가 나오지 않고,

C:\temp\output> ffprobe c:\temp\output\test_cs2.dat
ffprobe version 4.4.1 Copyright (c) 2007-2021 the FFmpeg developers
  built with Microsoft (R) C/C++ Optimizing Compiler Version 19.30.30705 for x64
  configuration: ...[생략]... --extra-cxxflags=-MD
  libavutil      56. 70.100 / 56. 70.100
  ...[생략]...
  libpostproc    55.  9.100 / 55.  9.100
[luodat @ 0000026A8014D640] Format luodat detected only with low score of 1, misdetection possible!
Input #0, luodat, from 'c:\temp\output\test_cs2.dat':
  Duration: N/A, bitrate: N/A

대신 ffplay로 재생할 때 pcm_f32le라고 나옵니다.

C:\temp\output> ffplay -f f32le -ac 2 -ar 44100 c:\temp\output\test_cs2.dat
ffplay version 4.4.1 Copyright (c) 2003-2021 the FFmpeg developers
  built with Microsoft (R) C/C++ Optimizing Compiler Version 19.30.30705 for x64
  configuration: ...[생략]... --extra-cxxflags=-MD
  libavutil      56. 70.100 / 56. 70.100
  ...[생략]...
  libpostproc    55.  9.100 / 55.  9.100
[f32le @ 000001FFFE6A5340] Estimating duration from bitrate, this may be inaccurate
Input #0, f32le, from 'c:\temp\output\test_cs2.dat':
  Duration: 00:04:00.35, bitrate: 2822 kb/s
  Stream #0:0: Audio: pcm_f32le, 44100 Hz, 2 channels, flt, 2822 kb/s
  49.46 M-A:  0.000 fd=   0 aq=  349KB vq=    0KB sq=    0B f=0/0

어쨌든, 뭔가 석연치 않은 점들이 있지만 일단 MP3 파일에 대한 디코딩/인코딩은 완료했습니다. ^^

(첨부 파일은 MP3 디코딩/인코딩 예제 코드를 포함합니다.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 1/25/2022]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 76  77  78  79  [80]  81  82  83  84  85  86  87  88  89  90  ...
NoWriterDateCnt.TitleFile(s)
11936정성태6/10/201918318Math: 58. C# - 최소 자승법의 1차, 2차 수렴 그래프 변화 확인 [2]파일 다운로드1
11935정성태6/9/201919871.NET Framework: 843. C# - PLplot 출력을 파일이 아닌 Window 화면으로 변경
11934정성태6/7/201921201VC++: 133. typedef struct와 타입 전방 선언으로 인한 C2371 오류파일 다운로드1
11933정성태6/7/201919552VC++: 132. enum 정의를 C++11의 enum class로 바꿀 때 유의할 사항파일 다운로드1
11932정성태6/7/201918738오류 유형: 544. C++ - fatal error C1017: invalid integer constant expression파일 다운로드1
11931정성태6/6/201919269개발 환경 구성: 441. C# - CairoSharp/GtkSharp 사용을 위한 프로젝트 구성 방법
11930정성태6/5/201919798.NET Framework: 842. .NET Reflection을 대체할 System.Reflection.Metadata 소개 [1]
11929정성태6/5/201919354.NET Framework: 841. Windows Forms/C# - 클립보드에 RTF 텍스트를 복사 및 확인하는 방법 [1]
11928정성태6/5/201918139오류 유형: 543. PowerShell 확장 설치 시 "Catalog file '[...].cat' is not found in the contents of the module" 오류 발생
11927정성태6/5/201919326스크립트: 15. PowerShell ISE의 스크립트를 복사 후 PPT/Word에 붙여 넣으면 한글이 깨지는 문제 [1]
11926정성태6/4/201919912오류 유형: 542. Visual Studio - pointer to incomplete class type is not allowed
11925정성태6/4/201919725VC++: 131. Visual C++ - uuid 확장 속성과 __uuidof 확장 연산자파일 다운로드1
11924정성태5/30/201921340Math: 57. C# - 해석학적 방법을 이용한 최소 자승법 [1]파일 다운로드1
11923정성태5/30/201920980Math: 56. C# - 그래프 그리기로 알아보는 경사 하강법의 최소/최댓값 구하기파일 다운로드1
11922정성태5/29/201918514.NET Framework: 840. ML.NET 데이터 정규화파일 다운로드1
11921정성태5/28/201924361Math: 55. C# - 다항식을 위한 최소 자승법(Least Squares Method)파일 다운로드1
11920정성태5/28/201916032.NET Framework: 839. C# - PLplot 색상 제어
11919정성태5/27/201920277Math: 54. C# - 최소 자승법의 1차 함수에 대한 매개변수를 단순 for 문으로 구하는 방법 [1]파일 다운로드1
11918정성태5/25/201921129Math: 53. C# - 행렬식을 이용한 최소 자승법(LSM: Least Square Method)파일 다운로드1
11917정성태5/24/201922094Math: 52. MathNet을 이용한 간단한 통계 정보 처리 - 분산/표준편차파일 다운로드1
11916정성태5/24/201919927Math: 51. MathNET + OxyPlot을 이용한 간단한 통계 정보 처리 - Histogram파일 다운로드1
11915정성태5/24/201923051Linux: 11. 리눅스의 환경 변수 관련 함수 정리 - putenv, setenv, unsetenv
11914정성태5/24/201921997Linux: 10. 윈도우의 GetTickCount와 리눅스의 clock_gettime파일 다운로드1
11913정성태5/23/201918752.NET Framework: 838. C# - 숫자형 타입의 bit(2진) 문자열, 16진수 문자열 구하는 방법파일 다운로드1
11912정성태5/23/201918699VS.NET IDE: 137. Visual Studio 2019 버전 16.1부터 리눅스 C/C++ 프로젝트에 추가된 WSL 지원
11911정성태5/23/201917478VS.NET IDE: 136. Visual Studio 2019 - 리눅스 C/C++ 프로젝트에 인텔리센스가 동작하지 않는 경우
... 76  77  78  79  [80]  81  82  83  84  85  86  87  88  89  90  ...