From 20d28e80a5c861a9d5f449ea911ab75b4f37ad0d Mon Sep 17 00:00:00 2001 From: Jef Date: Tue, 24 Sep 2024 14:54:57 +0200 Subject: Initial community commit --- Src/h264/h264_mp4_decoder.cpp | 212 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 Src/h264/h264_mp4_decoder.cpp (limited to 'Src/h264/h264_mp4_decoder.cpp') diff --git a/Src/h264/h264_mp4_decoder.cpp b/Src/h264/h264_mp4_decoder.cpp new file mode 100644 index 00000000..afb48bb3 --- /dev/null +++ b/Src/h264/h264_mp4_decoder.cpp @@ -0,0 +1,212 @@ +#include "h264_mp4_decoder.h" +#include "../winamp/wa_ipc.h" +#include +uint32_t Read24(const uint8_t *data) +{ + // ugh, 24bit size + uint32_t this_size=0; + uint8_t *this_size_p = (uint8_t *)&this_size; + this_size_p[0] = data[2]; + this_size_p[1] = data[1]; + this_size_p[2] = data[0]; + return this_size; +} + +uint32_t GetNALUSize(uint64_t nalu_size_bytes, const uint8_t *h264_data, size_t data_len) +{ + if ((data_len) < (nalu_size_bytes)) + return 0; + + switch(nalu_size_bytes) + { + case 1: + return *h264_data; + case 2: + { + return (h264_data[0] << 8) | h264_data[1]; + + } + case 3: + { + return Read24(h264_data); + } + case 4: + { + uint32_t this_size = *(uint32_t *)h264_data; + this_size = htonl(this_size); + return this_size; + } + } + return 0; +} + +H264MP4Decoder::H264MP4Decoder() +{ + nalu_size_bytes=0; + width=0; + height=0; +} + +H264MP4Decoder::~H264MP4Decoder() +{ + for (size_t i=0;imp4_file=mp4_file; + this->mp4_track=mp4_track; + decoder.Open(); + // TODO error checking + uint8_t **seqHeaders = 0, **pictHeaders = 0; + uint32_t *seqHeadersSize = 0, *pictHeadersSize = 0; + + __try + { + MP4GetTrackH264SeqPictHeaders(mp4_file, mp4_track, + &seqHeaders, &seqHeadersSize, + &pictHeaders, &pictHeadersSize); + + if (seqHeadersSize) + { + for (uint32_t i = 0; seqHeadersSize[i] != 0; i++) + { + decoder.Feed(seqHeaders[i], seqHeadersSize[i], 0); + MP4Free(seqHeaders[i]); + } + } + MP4Free(seqHeadersSize); + + if (pictHeadersSize) + { + for (uint32_t i = 0; pictHeadersSize[i] != 0; i++) + { + decoder.Feed(pictHeaders[i], pictHeadersSize[i], 0); + MP4Free(pictHeaders[i]); + } + } + MP4Free(pictHeadersSize); + + MP4GetTrackH264LengthSize(mp4_file, mp4_track, &nalu_size_bytes); + } + __except(EXCEPTION_EXECUTE_HANDLER) + { + return MP4_VIDEO_FAILURE; + } + + return MP4_VIDEO_SUCCESS; +} + +int H264MP4Decoder::GetOutputFormat(int *x, int *y, int *color_format, double *aspect_ratio) +{ + bool flip; + + if (SUCCEEDED(decoder.GetOutputFormat(&width, &height, &flip, aspect_ratio))) { + *x = width; + *y = height; + *color_format = htonl('YV12'); + return MP4_VIDEO_SUCCESS; + } + return MP4_VIDEO_FAILURE; +} + + +int H264MP4Decoder::DecodeSample(const void *inputBuffer, size_t inputBufferBytes, MP4Timestamp timestamp) +{ + const uint8_t *h264_data = (const uint8_t *)inputBuffer; + while (inputBufferBytes) + { + uint32_t this_size =GetNALUSize(nalu_size_bytes, h264_data, inputBufferBytes); + if (this_size == 0) + return MP4_VIDEO_FAILURE; + + inputBufferBytes-=nalu_size_bytes; + h264_data+=nalu_size_bytes; + if (this_size > inputBufferBytes) + return MP4_VIDEO_FAILURE; + + for (;;) { + uint64_t hundrednanos = MP4ConvertFromTrackTimestamp(mp4_file, mp4_track, timestamp, MP4_NANOSECONDS_TIME_SCALE/100ULL); + HRESULT hr = decoder.Feed(h264_data, this_size, hundrednanos); + if (hr == MF_E_NOTACCEPTING) { + nullsoft_h264_frame_data frame_data; + if (FAILED(decoder.GetFrame((YV12_PLANES **)&frame_data.data, &frame_data.decoder_data, &frame_data.local_timestamp))) { + continue; + } + buffered_frames.push_back(frame_data); + } else if (FAILED(hr)) { + return MP4_VIDEO_FAILURE; + } else { + break; + } + } + + inputBufferBytes-=this_size; + h264_data+=this_size; + } + return MP4_VIDEO_SUCCESS; +} + +int H264MP4Decoder::CanHandleCodec(const char *codecName) +{ + return !strcmp(codecName, "avc1"); +} + +void H264MP4Decoder::Flush() +{ + for (size_t i=0;i