15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef MEDIA_FILTERS_H264_TO_ANNEX_B_BITSTREAM_CONVERTER_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define MEDIA_FILTERS_H264_TO_ANNEX_B_BITSTREAM_CONVERTER_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "media/base/media_export.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace media { 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// H264ToAnnexBBitstreamConverter is a class to convert H.264 bitstream from 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// MP4 format (as specified in ISO/IEC 14496-15) into H.264 bytestream 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (as specified in ISO/IEC 14496-10 Annex B). 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MEDIA_EXPORT H264ToAnnexBBitstreamConverter { 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) H264ToAnnexBBitstreamConverter(); 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~H264ToAnnexBBitstreamConverter(); 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Parses the global AVCDecoderConfigurationRecord from the file format's 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // headers. Converter will remember the field length from the configuration 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // headers after this. 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Parameters 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // configuration_record 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Pointer to buffer containing AVCDecoderConfigurationRecord. 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // configuration_record_size 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Size of the buffer in bytes. 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Required buffer size for AVCDecoderConfigurationRecord when converted 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to bytestream format, or 0 if could not determine the configuration 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // from the input buffer. 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint32 ParseConfigurationAndCalculateSize(const uint8* configuration_record, 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint32 configuration_record_size); 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Calculates needed buffer size for the bitstream converted into bytestream. 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Lightweight implementation that does not do the actual conversion. 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Parameters 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // configuration_record 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Pointer to buffer containing AVCDecoderConfigurationRecord. 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // configuration_record_size 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Size of the buffer in bytes. 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Required buffer size for the input NAL unit buffer when converted 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to bytestream format, or 0 if could not determine the configuration 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // from the input buffer. 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint32 CalculateNeededOutputBufferSize(const uint8* input, 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint32 input_size) const; 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ConvertAVCDecoderConfigToByteStream converts the 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // AVCDecoderConfigurationRecord from the MP4 headers to bytestream format. 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Client is responsible for making sure the output buffer is large enough 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to hold the output data. Client can precalculate the needed output buffer 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // size by using ParseConfigurationAndCalculateSize. 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // In case of failed conversion object H264BitstreamConverter may have written 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // some bytes to buffer pointed by pinput but user should ignore those bytes. 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // None of the outputs should be considered valid. 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Parameters 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // pinput 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Pointer to buffer containing AVCDecoderConfigurationRecord. 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // input_size 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Size of the buffer in bytes. 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // poutput 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Pointer to buffer where the output should be written to. 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // poutput_size (i/o) 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Pointer to the size of the output buffer. Will contain the number of 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // bytes written to output after successful call. 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // true if successful conversion 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // false if conversion not successful (poutput_size will hold the amount 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // of converted data) 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool ConvertAVCDecoderConfigToByteStream(const uint8* input, 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint32 input_size, 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint8* output, 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint32* output_size); 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ConvertNalUnitStreamToByteStream converts the NAL unit from MP4 format 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to bytestream format. Client is responsible for making sure the output 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // buffer is large enough to hold the output data. Client can precalculate the 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // needed output buffer size by using CalculateNeededOutputBufferSize. 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // In case of failed conversion object H264BitstreamConverter may have written 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // some bytes to buffer pointed by pinput but user should ignore those bytes. 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // None of the outputs should be considered valid. 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Parameters 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // pinput 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Pointer to buffer containing AVCDecoderConfigurationRecord. 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // input_size 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Size of the buffer in bytes. 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // poutput 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Pointer to buffer where the output should be written to. 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // poutput_size (i/o) 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Pointer to the size of the output buffer. Will contain the number of 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // bytes written to output after successful call. 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // true if successful conversion 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // false if conversion not successful (poutput_size will hold the amount 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // of converted data) 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool ConvertNalUnitStreamToByteStream(const uint8* input, uint32 input_size, 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint8* output, uint32* output_size); 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Flag for indicating whether global parameter sets have been processed. 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool configuration_processed_; 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Flag for indicating whether next NAL unit starts new access unit. 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool first_nal_unit_in_access_unit_; 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Variable to hold interleaving field's length in bytes. 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint8 nal_unit_length_field_width_; 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(H264ToAnnexBBitstreamConverter); 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace media 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // MEDIA_FILTERS_H264_TO_ANNEX_B_BITSTREAM_CONVERTER_H_ 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 126