#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