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

비밀번호

댓글 작성자
 




... 61  62  63  64  65  66  [67]  68  69  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
12258정성태7/8/202020167기타: 77. DataDog APM 간략 소개
12257정성태7/7/202016467.NET Framework: 925. C# - ETW를 이용한 Monitor Enter/Exit 감시파일 다운로드1
12256정성태7/7/202017591.NET Framework: 924. C# - Reflection으로 변경할 수 없는 readonly 정적 필드 [4]
12255정성태7/6/202018376.NET Framework: 923. C# - ETW(Event Tracing for Windows)를 이용한 Finalizer 실행 감시파일 다운로드1
12254정성태7/2/202016893오류 유형: 626. git - REMOTE HOST IDENTIFICATION HAS CHANGED!
12253정성태7/2/202019381.NET Framework: 922. C# - .NET ThreadPool의 Local/Global Queue파일 다운로드1
12252정성태7/2/202021179.NET Framework: 921. C# - I/O 스레드를 사용한 비동기 소켓 서버/클라이언트파일 다운로드2
12251정성태7/1/202019956.NET Framework: 920. C# - 파일의 비동기 처리 유무에 따른 스레드 상황 [1]파일 다운로드2
12250정성태6/30/202022114.NET Framework: 919. C# - 닷넷에서의 진정한 비동기 호출을 가능케 하는 I/O 스레드 사용법 [1]파일 다운로드1
12249정성태6/29/202017534오류 유형: 625. Microsoft SQL Server 2019 RC1 Setup - 설치 제거 시 Warning 26003 오류 발생
12248정성태6/29/202015340오류 유형: 624. SQL 서버 오류 - service-specific error code 17051
12247정성태6/29/202017272.NET Framework: 918. C# - 불린 형 상수를 반환값으로 포함하는 3항 연산자 사용 시 단축 표현 권장(IDE0075) [2]파일 다운로드1
12246정성태6/29/202018434.NET Framework: 917. C# - USB 관련 ETW(Event Tracing for Windows)를 이용한 키보드 입력을 감지하는 방법
12245정성태6/24/202018864.NET Framework: 916. C# - Task.Yield 사용법 (2) [2]파일 다운로드1
12244정성태6/24/202018896.NET Framework: 915. ETW(Event Tracing for Windows)를 이용한 닷넷 프로그램의 내부 이벤트 활용 [1]파일 다운로드1
12243정성태6/23/202015324VS.NET IDE: 147. Visual C++ 프로젝트 - .NET Core EXE를 "Debugger Type"으로 지원하는 기능 추가
12242정성태6/23/202016901오류 유형: 623. AADSTS90072 - User account '...' from identity provider 'live.com' does not exist in tenant 'Microsoft Services'
12241정성태6/23/202020120.NET Framework: 914. C# - Task.Yield 사용법파일 다운로드1
12240정성태6/23/202021675오류 유형: 622. 소켓 바인딩 시 "System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions" 오류 발생
12239정성태6/21/202019684Linux: 30. (윈도우라면 DLL에 속하는) .so 파일이 텍스트로 구성된 사례 [1]
12238정성태6/21/202018072.NET Framework: 913. C# - SharpDX + DXGI를 이용한 윈도우 화면 캡처 라이브러리
12237정성태6/20/202017576.NET Framework: 912. 리눅스 환경의 .NET Core에서 "test".IndexOf("\0")가 0을 반환
12236정성태6/19/202018308오류 유형: 621. .NET Standard 대상으로 빌드 시 dynamic 예약어에서 컴파일 오류 - error CS0656: Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'
12235정성태6/19/202017486오류 유형: 620. Windows 10 - Inaccessible boot device 블루 스크린
12234정성태6/19/202016882개발 환경 구성: 494. NuGet - nuspec의 패키지 스키마 버전(네임스페이스) 업데이트 방법
12233정성태6/19/202017336오류 유형: 619. SQL 서버 - The transaction log for database '...' is full due to 'LOG_BACKUP'. - 두 번째 이야기
... 61  62  63  64  65  66  [67]  68  69  70  71  72  73  74  75  ...