Especificação



Baixar 2,81 Mb.
Página13/24
Encontro01.07.2018
Tamanho2,81 Mb.
1   ...   9   10   11   12   13   14   15   16   ...   24

A4 Classes de codificação 1 – Controle




      1. A4.1 Classe H264Encoder



package br.ufsc.inf.guiga.media.codec.video.h264;
import java.util.logging.Handler;

import java.util.logging.Logger;
import javax.media.Buffer;

import javax.media.Control;

import javax.media.Format;

import javax.media.format.YUVFormat;
import br.ufsc.inf.guiga.media.Global;

import br.ufsc.inf.guiga.media.Registry;

import br.ufsc.inf.guiga.media.codec.video.h264.vcl.Picture;

import br.ufsc.inf.guiga.media.codec.video.h264.vcl.VideoSequence;

import br.ufsc.inf.guiga.media.control.H264Adapter;

import br.ufsc.inf.guiga.media.control.H264Control;

import br.ufsc.inf.guiga.media.format.H264Format;

import br.ufsc.inf.guiga.media.parser.video.YUVFormatHandler;
import com.sun.media.BasicCodec;
/**

* H.264/AVC Encoder.

*

* Process YUV video data, encoding into H.264/AVC coded video data.

*

* @author Guilherme Ferreira



*/

public class H264Encoder extends BasicCodec {
private static final String PLUGIN_NAME = "H.264/AVC Encoder";

private VideoSequence videoSequence;

private Logger logger;
// -------------------------------------------------------------------------

// DATA INITIALIZATION

// -------------------------------------------------------------------------
/**

* Register this encoder within JMF

*/

public static void register() {

Registry.registerPackage();

Registry.registerYUVHandler();

Registry.registerH264Encoder();

}
/**

* Create a H.264 Encoder with default configuration.

*/

public H264Encoder() {

init();


}
/**

* Create a H.264 Encoder with default configuration and a given {@link Handler} to

* log the messages from this encoder.

*

* @param handler a {@link Handler} to print log messages.



*/

public H264Encoder(Handler handler) {

init();
logger.addHandler(handler);

}
private void init() {

YUVFormat sqcif = YUVFormatHandler.getSQCIF(YUVFormat.YUV_420, 15);

YUVFormat qcif = YUVFormatHandler.getQCIF(YUVFormat.YUV_420, 15);

YUVFormat cif = YUVFormatHandler.getCIF(YUVFormat.YUV_420, 15);

YUVFormat _4cif = YUVFormatHandler.get4CIF(YUVFormat.YUV_420, 15);
inputFormats = new Format[] { sqcif, qcif, cif, _4cif };
outputFormats = new Format[] { H264Format.getMatchFormat(sqcif),

H264Format.getMatchFormat(qcif), H264Format.getMatchFormat(cif),

H264Format.getMatchFormat(_4cif) };
Global.getInstance().setH264Control(new H264Adapter());
logger = Logger.getLogger(getClass().getSimpleName());

}
// -------------------------------------------------------------------------

// DATA FORMAT CONFIGURATION

// -------------------------------------------------------------------------


/**

* Lists the output formats that this CODEC can generate. If input is non-null, this

* method lists the possible output formats that can be generated from input data of

* the specified {@link Format}. If input is null, this method lists

* all of the output formats supported by this plug-in.

*

* @param input The {@link Format} of the data to be used as input to the plug-in.



* @return An array that contains the supported output Formats.

*/

public Format[] getSupportedOutputFormats(Format input) {

Format[] supportedFormats = {};
if (input == null) {

supportedFormats = outputFormats;

} else {

if (input instanceof YUVFormat) {

supportedFormats = new Format[1];

supportedFormats[0] = H264Format.getMatchFormat((YUVFormat) input);

}

}


return supportedFormats;

}
/**

* Sets the format of the data to be input to this CODEC.

*

* @param format The {@link Format} to be set.



* @return The {@link Format} that was set, which might be the supported

* {@link Format} that most closely matches the one specified. Returns null if

* the specified {@link Format} is not supported and no reasonable match could

* be found.

*/

public Format setInputFormat(Format input) {

if (input instanceof YUVFormat) {

videoSequence = new VideoSequence((YUVFormat) input);


return super.setInputFormat(input);

} else {



return null;

}

}


/**

* Gets the name of this plug-in as a human-readable string.

*

* @return A String that contains the descriptive name of the plug-in.



*/

public String getName() {

return PLUGIN_NAME;

}
/**

* Obtain the collection of objects that control the object that implements this

* interface. If no controls are supported, a zero length array is returned.

*

* @return the collection of object controls



*/

public Object[] getControls() {

Control[] controls = { Global.getInstance().getH264Control() };


return controls;

}
/**

* Obtain the object that implements the specified Class or

* Interface The full class or interface name must be used. If the

* control is not supported then null is returned.

*

* @return the object that implements the control, or null.



*/

public Object getControl(String controlType) {

Object cls = null;


try {

cls = Class.forName(controlType);


} catch (ClassNotFoundException e) {

return null;

}
if (cls instanceof H264Control) {



return Global.getInstance().getH264Control();

}
return null;

}
// -------------------------------------------------------------------------

// DATA PROCESSING

// -------------------------------------------------------------------------
/**

* Converts a byte array Buffer of raw YUV video data into an byte array Buffer of

* H.264/AVC (MPEG-4 Part 10) video data. Always inputs one raw video frame and

* outputs one coded frame.

*

* @return BUFFER_PROCESSED_OK The output buffer contains a valid coded



* frame

* BUFFER_PROCESSED_FAILED A encoding problem was encountered

*/

public int process(Buffer input, Buffer output) {

// look for a valid input buffer



if (!isInputSupported(input.getFormat())) {

return BUFFER_PROCESSED_FAILED;

}
// does this Buffer marks the End Of Media?



if (input.isEOM()) {

System.err.printf("Uncoded bytes left in input buffer: %d ", input

.getLength());

return BUFFER_PROCESSED_FAILED;

}
// code one frame, this method is called for each input frame read

encodeData(input, output);
logger.info("Coded Frame\n"); // TODO Required better log, it's just a test
return BUFFER_PROCESSED_OK;

}
/**

* @param inputBuffer

* @param outputBuffer

*/

private void encodeData(Buffer inputBuffer, Buffer outputBuffer) {

byte[] inputData = (byte[]) inputBuffer.getData();

Picture outputData = videoSequence.encode(inputData);

outputBuffer.setData(outputData);

}
// -------------------------------------------------------------------------

// DATA VERIFICATION AND VALIDATION

// -------------------------------------------------------------------------


/**

* Check if the input format is supported by this CODEC.

*/

private boolean isInputSupported(Format format) {

boolean supported = false;

for (int i = 0; i < inputFormats.length; i++) {

if (inputFormats[i].matches(format)) {

supported = true;

}

}

return supported;



}
}

      1. A4.2 Classe BaselineProfileFactory



package br.ufsc.inf.guiga.media.codec.video.h264;
import br.ufsc.inf.guiga.media.codec.video.h264.vcl.algorithm.AlgorithmFactory;

import br.ufsc.inf.guiga.media.codec.video.h264.vcl.algorithm.Quantizer;

import br.ufsc.inf.guiga.media.codec.video.h264.vcl.algorithm.Scanner;

import br.ufsc.inf.guiga.media.codec.video.h264.vcl.algorithm.Transform;

import br.ufsc.inf.guiga.media.codec.video.h264.vcl.algorithm.impl.IntegerRoundingQuantizer;

import br.ufsc.inf.guiga.media.codec.video.h264.vcl.algorithm.impl.IntegerTransform;

import br.ufsc.inf.guiga.media.codec.video.h264.vcl.algorithm.impl.ZigZagFrameScanner;

import br.ufsc.inf.guiga.media.codec.video.h264.vcl.mode.decision.DistortionMetric;

import br.ufsc.inf.guiga.media.codec.video.h264.vcl.mode.decision.SATD;
public class BaselineProfileFactory implements ProfileFactory, AlgorithmFactory {
private Transform transform;

private Quantizer quantizer;

private Scanner scanner;

private DistortionMetric distortionMetric;
/**

* @param qp the Quantization Parameter used by the macroblock.

*/

public BaselineProfileFactory(int qp) {

transform = new IntegerTransform();

scanner = new ZigZagFrameScanner();

quantizer = new IntegerRoundingQuantizer(qp, scanner);

distortionMetric = new SATD(transform);

}
public Quantizer createQuantizer() {



return quantizer;

}
public Scanner createScanner() {



return scanner;

}
public Transform createTransform() {



return transform;

}
public DistortionMetric createDistortionMetric() {



return distortionMetric;

}
}





1   ...   9   10   11   12   13   14   15   16   ...   24


©livred.info 2017
enviar mensagem

    Página principal