2

I am currently trying to apply lossless H264 settings for FFMPEG in c. However, I am unsure as to what the settings need to be to ensure lossless encoding and I have found little documentation in this area.

My current settings are:

    codecContex->coder_type = 1; 
    codecContex->flags|=CODEC_FLAG_LOOP_FILTER;
    codecContex->flags2|=CODEC_FLAG2_BPYRAMID-CODEC_FLAG2_WPRED-CODEC_FLAG2_8X8DCT;

    codecContex->profile=FF_PROFILE_H264_BASELINE;
    codecContex->scenechange_threshold = 40; 
    codecContex->gop_size=40;
    codecContex->max_b_frames=0;
    codecContex->max_qdiff=4;
    codecContex->me_method=10;
    codecContex->me_range=16;
    codecContex->me_cmp|= 1;
    codecContex->me_subpel_quality = 5; 
    codecContex->qmin=0; 
    codecContex->qmax=0;
    codecContex->qcompress=0.6f;
    codecContex->keyint_min=25;
    codecContex->trellis=0;
    codecContex->level=13;
    codecContex->refs = 16;
    codecContex->weighted_p_pred = 2;
    codecContex->b_frame_strategy= 1;
    codecContex->color_range = libffmpeg::AVCOL_RANGE_JPEG;
    codecContex->coder_type = FF_CODER_TYPE_AC;
    codecContex->crf = 0;

Any ideas as to what they should be to ensure lossless encoding? Thanks in advance.

Squid
  • 45
  • 1
  • 4

1 Answers1

2

Try this:

...
AVDictionary *param;
av_dict_set(&param, "qp", "0", 0);
/*
Change options to trade off compression efficiency against encoding speed. If you specify a preset, the changes it makes will be applied before all other parameters are applied.
You should generally set this option to the slowest you can bear.
Values available: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo.
*/
av_dict_set(&param, "preset", "medium", 0);
/*
Tune options to further optimize them for your input content. If you specify a tuning, the changes will be applied after --preset but before all other parameters.
If your source content matches one of the available tunings you can use this, otherwise leave unset.
Values available: film, animation, grain, stillimage, psnr, ssim, fastdecode, zerolatency.
*/
av_dict_set(&param, "tune", "film", 0);
int rt = avcodec_open2(codecContext, codec, &param);
...

For lossless don't use profile

luca
  • 150
  • 1
  • 9
  • Hi luca, I attempted this method, however, now when ever i run my C# code accessing using the DLL's produced by this code, i can an "AccessViolationUnhandled" error stating "Attempted to read or write protected memory. This is often an indication that other memory is corrupt.". I'm not really sure what causes this, any ideas? – Squid Mar 03 '14 at 02:58
  • sorry, have you tried to set param=0? because av_dict_set allocate memory if param=0 but if not it thinks that memory is allocated – luca Mar 03 '14 at 14:15
  • Hi Luca, Thanks so much for your help! I can now build and run the code, unfortunately it still doesn't seem to encode purely lossless though, since when I decode the file, it is smaller than expected (usually by about 25MB). – Squid Mar 05 '14 at 00:25
  • Sorry, it actually does look like the compression is working, the problem is elsewhere, to do with writing frames to the file. Thank you very much for your help! – Squid Mar 05 '14 at 02:50