summaryrefslogtreecommitdiffstats
path: root/HBMpeg4Encoder.cpp
blob: f8682330b25d493700b93655044b81b92407e108 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* $Id: HBMpeg4Encoder.cpp,v 1.18 2003/08/23 19:38:47 titer Exp $ */

#include "HBCommon.h"
#include "HBMpeg4Encoder.h"
#include "HBManager.h"
#include "HBFifo.h"

#include <ffmpeg/avcodec.h>

HBMpeg4Encoder::HBMpeg4Encoder( HBManager * manager, HBTitleInfo * titleInfo )
    : HBThread( "mpeg4encoder" )
{
    fManager   = manager;
    fTitleInfo = titleInfo;
}

void HBMpeg4Encoder::DoWork()
{
    /* Init libavcodec */
    AVCodec * codec = avcodec_find_encoder( CODEC_ID_MPEG4 );
    if( !codec )
    {
        Log( "HBMpeg4Encoder: avcodec_find_encoder() failed" );
        fManager->Error();
        return;
    }

#define WIDTH  fTitleInfo->fOutWidth
#define HEIGHT fTitleInfo->fOutHeight
#define RATE   fTitleInfo->fRate
#define SCALE  fTitleInfo->fScale

    AVCodecContext * context;
    context                      = avcodec_alloc_context();
    context->bit_rate            = 1024 * fTitleInfo->fBitrate;
    context->bit_rate_tolerance  = 1024 * fTitleInfo->fBitrate;
    context->flags              |= CODEC_FLAG_HQ;
    context->width               = WIDTH;
    context->height              = HEIGHT;
    context->frame_rate          = RATE;
    context->frame_rate_base     = SCALE;
    context->gop_size            = 10 * RATE / SCALE;
    
    if( avcodec_open( context, codec ) < 0 )
    {
        Log( "HBMpeg4Encoder: avcodec_open() failed" );
        fManager->Error();
        return;
    }

    AVFrame  * frame = avcodec_alloc_frame();
    HBBuffer * mpeg4Buffer;

    for( ;; )
    {
        /* Get another frame */
        if( !( fRawBuffer = fTitleInfo->fRawFifo->Pop() ) )
            break;

        frame->data[0] = fRawBuffer->fData;
        frame->data[1] = frame->data[0] + WIDTH * HEIGHT;
        frame->data[2] = frame->data[1] + WIDTH * HEIGHT / 4;
        frame->linesize[0] = WIDTH;
        frame->linesize[1] = WIDTH / 2;
        frame->linesize[2] = WIDTH / 2;

        mpeg4Buffer = new HBBuffer( 3 * WIDTH * HEIGHT / 2 );
            /* Should be too much. It can't be bigger than the raw video ! */

        mpeg4Buffer->fSize =
            avcodec_encode_video( context, mpeg4Buffer->fData,
                                  mpeg4Buffer->fAllocSize, frame );
        mpeg4Buffer->fKeyFrame = ( context->coded_frame->key_frame != 0 );
        
#undef WIDTH
#undef HEIGHT
#undef RATE
#undef SCALE

        delete fRawBuffer;

        /* Mux it */
        if( !fTitleInfo->fMpeg4Fifo->Push( mpeg4Buffer ) )
        {
            break;
        }
    }
}