~ubuntu-branches/ubuntu/utopic/zoneminder/utopic-proposed

« back to all changes in this revision

Viewing changes to .pc/fix-ftbfs-libav9.patch/src/zm_mpeg.cpp

  • Committer: Package Import Robot
  • Author(s): Ricardo Salveti de Araujo
  • Date: 2014-01-19 22:07:18 UTC
  • mfrom: (15.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20140119220718-idkyepfw1zvr6cm2
Tags: 1.26.5-1ubuntu1
* Merge from Debian unstable. Remaining changes:
  - debian/control: Add libnet-sftp-foreign-perl to dependencies.
  - debian/control: Apache2 transition.
  - Port from ffmpeg to avconv.
  - Still build-depending on libgnutls-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * ZoneMinder MPEG class implementation, $Date: 2011-05-15 21:38:51 +0100 (Sun, 15 May 2011) $, $Revision: 3338 $
3
 
 * Copyright (C) 2001-2008 Philip Coombes
4
 
 * 
5
 
 * This program is free software; you can redistribute it and/or
6
 
 * modify it under the terms of the GNU General Public License
7
 
 * as published by the Free Software Foundation; either version 2
8
 
 * of the License, or (at your option) any later version.
9
 
 * 
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 * 
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program; if not, write to the Free Software
17
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
 
*/ 
19
 
 
20
 
#include <stdlib.h>
21
 
#include <string.h>
22
 
 
23
 
#include "zm.h"
24
 
#include "zm_mpeg.h"
25
 
 
26
 
#if HAVE_LIBAVCODEC
27
 
 
28
 
bool VideoStream::initialised = false;
29
 
 
30
 
VideoStream::MimeData VideoStream::mime_data[] = {
31
 
        { "asf", "video/x-ms-asf" },
32
 
        { "swf", "application/x-shockwave-flash" },
33
 
        { "flv", "video/x-flv" },
34
 
        { "mp4", "video/mp4" },
35
 
        { "move", "video/quicktime" }
36
 
};
37
 
 
38
 
void VideoStream::Initialise()
39
 
{
40
 
        av_register_all();
41
 
        initialised = true;
42
 
}
43
 
 
44
 
void VideoStream::SetupFormat( const char *p_filename, const char *p_format )
45
 
{
46
 
        filename = p_filename;
47
 
        format = p_format;
48
 
 
49
 
        /* auto detect the output format from the name. default is mpeg. */
50
 
        of = av_guess_format( format, NULL, NULL);
51
 
        if ( !of )
52
 
        {
53
 
                Warning( "Could not deduce output format from file extension: using mpeg" );
54
 
                of = av_guess_format("mpeg", NULL, NULL);
55
 
        }
56
 
        if ( !of )
57
 
        {
58
 
                Fatal( "Could not find suitable output format" );
59
 
        }
60
 
        
61
 
        /* allocate the output media context */
62
 
        ofc = (AVFormatContext *)av_mallocz(sizeof(AVFormatContext));
63
 
        if ( !ofc )
64
 
        {
65
 
                Panic( "Memory error" );
66
 
        }
67
 
        ofc->oformat = of;
68
 
        snprintf( ofc->filename, sizeof(ofc->filename), "%s", filename );
69
 
}
70
 
 
71
 
void VideoStream::SetupCodec( int colours, int width, int height, int bitrate, double frame_rate )
72
 
{
73
 
        pf = (colours==1?PIX_FMT_GRAY8:PIX_FMT_RGB24);
74
 
 
75
 
        /* add the video streams using the default format codecs
76
 
           and initialize the codecs */
77
 
        ost = NULL;
78
 
        if (of->video_codec != CODEC_ID_NONE)
79
 
        {
80
 
                ost = av_new_stream(ofc, 0);
81
 
                if (!ost)
82
 
                {
83
 
                        Panic( "Could not alloc stream" );
84
 
                }
85
 
                
86
 
#if ZM_FFMPEG_SVN
87
 
                AVCodecContext *c = ost->codec;
88
 
#else
89
 
                AVCodecContext *c = &ost->codec;
90
 
#endif
91
 
 
92
 
                c->codec_id = of->video_codec;
93
 
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(51,2,1)
94
 
                c->codec_type = AVMEDIA_TYPE_VIDEO;
95
 
#else
96
 
                c->codec_type = CODEC_TYPE_VIDEO;
97
 
#endif
98
 
 
99
 
                /* put sample parameters */
100
 
                c->bit_rate = bitrate;
101
 
                c->pix_fmt = PIX_FMT_YUV420P;
102
 
                /* resolution must be a multiple of two */
103
 
                c->width = width;
104
 
                c->height = height;
105
 
#if ZM_FFMPEG_SVN
106
 
                /* time base: this is the fundamental unit of time (in seconds) in terms
107
 
                   of which frame timestamps are represented. for fixed-fps content,
108
 
                   timebase should be 1/framerate and timestamp increments should be
109
 
                   identically 1. */
110
 
                //c->time_base.den = (int)(frame_rate*100);
111
 
                //c->time_base.num = 100;
112
 
                c->time_base.den = frame_rate;
113
 
                c->time_base.num = 1;
114
 
#else
115
 
                /* frames per second */
116
 
                c->frame_rate = frame_rate;
117
 
                c->frame_rate_base = 1;
118
 
#endif
119
 
                //c->gop_size = frame_rate/2; /* emit one intra frame every half second or so */
120
 
                c->gop_size = 12;
121
 
                if ( c->gop_size < 3 )
122
 
                        c->gop_size = 3;
123
 
                // some formats want stream headers to be seperate
124
 
                if(!strcmp(ofc->oformat->name, "mp4") || !strcmp(ofc->oformat->name, "mov") || !strcmp(ofc->oformat->name, "3gp"))
125
 
                        c->flags |= CODEC_FLAG_GLOBAL_HEADER;
126
 
        }
127
 
}
128
 
 
129
 
void VideoStream::SetParameters()
130
 
{
131
 
        /* set the output parameters (must be done even if no
132
 
           parameters). */
133
 
        if ( av_set_parameters(ofc, NULL) < 0 )
134
 
        {
135
 
                Panic( "Invalid output format parameters" );
136
 
        }
137
 
        //dump_format(ofc, 0, filename, 1);
138
 
}
139
 
 
140
 
const char *VideoStream::MimeType() const
141
 
{
142
 
        for ( int i = 0; i < sizeof(mime_data)/sizeof(*mime_data); i++ )
143
 
        {
144
 
                if ( strcmp( format, mime_data[i].format ) == 0 )
145
 
                {
146
 
                        return( mime_data[i].mime_type );
147
 
                }
148
 
        }
149
 
        const char *mime_type = of->mime_type;
150
 
        if ( !mime_type )
151
 
        {
152
 
                mime_type = "video/mpeg";
153
 
                Warning( "Unable to determine mime type for '%s' format, using '%s' as default", format, mime_type );
154
 
        }
155
 
 
156
 
        return( mime_type );
157
 
}
158
 
 
159
 
void VideoStream::OpenStream()
160
 
{
161
 
        /* now that all the parameters are set, we can open the 
162
 
           video codecs and allocate the necessary encode buffers */
163
 
        if ( ost )
164
 
        {
165
 
#if ZM_FFMPEG_SVN
166
 
                AVCodecContext *c = ost->codec;
167
 
#else
168
 
                AVCodecContext *c = &ost->codec;
169
 
#endif
170
 
 
171
 
                /* find the video encoder */
172
 
                AVCodec *codec = avcodec_find_encoder(c->codec_id);
173
 
                if ( !codec )
174
 
                {
175
 
                        Panic( "codec not found" );
176
 
                }
177
 
 
178
 
                /* open the codec */
179
 
                if ( avcodec_open(c, codec) < 0 )
180
 
                {
181
 
                        Panic( "Could not open codec" );
182
 
                }
183
 
 
184
 
                /* allocate the encoded raw picture */
185
 
                opicture = avcodec_alloc_frame();
186
 
                if ( !opicture )
187
 
                {
188
 
                        Panic( "Could not allocate opicture" );
189
 
                }
190
 
                int size = avpicture_get_size( c->pix_fmt, c->width, c->height);
191
 
                uint8_t *opicture_buf = (uint8_t *)malloc(size);
192
 
                if ( !opicture_buf )
193
 
                {
194
 
                        av_free(opicture);
195
 
                        Panic( "Could not allocate opicture" );
196
 
                }
197
 
                avpicture_fill( (AVPicture *)opicture, opicture_buf, c->pix_fmt, c->width, c->height );
198
 
 
199
 
                /* if the output format is not RGB24, then a temporary RGB24
200
 
                   picture is needed too. It is then converted to the required
201
 
                   output format */
202
 
                tmp_opicture = NULL;
203
 
                if ( c->pix_fmt != pf )
204
 
                {
205
 
                        tmp_opicture = avcodec_alloc_frame();
206
 
                        if ( !tmp_opicture )
207
 
                        {
208
 
                                Panic( "Could not allocate temporary opicture" );
209
 
                        }
210
 
                        int size = avpicture_get_size( pf, c->width, c->height);
211
 
                        uint8_t *tmp_opicture_buf = (uint8_t *)malloc(size);
212
 
                        if (!tmp_opicture_buf)
213
 
                        {
214
 
                                av_free( tmp_opicture );
215
 
                                Panic( "Could not allocate temporary opicture" );
216
 
                        }
217
 
                        avpicture_fill( (AVPicture *)tmp_opicture, tmp_opicture_buf, pf, c->width, c->height );
218
 
                }
219
 
        }
220
 
 
221
 
        /* open the output file, if needed */
222
 
        if ( !(of->flags & AVFMT_NOFILE) )
223
 
        {
224
 
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(51,2,1)
225
 
                if ( avio_open(&ofc->pb, filename, URL_WRONLY) < 0 )
226
 
#else
227
 
                if ( url_fopen(&ofc->pb, filename, URL_WRONLY) < 0 )
228
 
#endif
229
 
                {
230
 
                        Fatal( "Could not open '%s'", filename );
231
 
                }
232
 
        }
233
 
 
234
 
        video_outbuf = NULL;
235
 
        if ( !(ofc->oformat->flags & AVFMT_RAWPICTURE) )
236
 
        {
237
 
                /* allocate output buffer */
238
 
                /* XXX: API change will be done */
239
 
                video_outbuf_size = 200000;
240
 
                video_outbuf = (uint8_t *)malloc(video_outbuf_size);
241
 
        }
242
 
 
243
 
        /* write the stream header, if any */
244
 
        av_write_header(ofc);
245
 
}
246
 
 
247
 
VideoStream::VideoStream( const char *filename, const char *format, int bitrate, double frame_rate, int colours, int width, int height )
248
 
{
249
 
        if ( !initialised )
250
 
        {
251
 
                Initialise();
252
 
        }
253
 
 
254
 
        SetupFormat( filename, format );
255
 
        SetupCodec( colours, width, height, bitrate, frame_rate );
256
 
        SetParameters();
257
 
}
258
 
 
259
 
VideoStream::~VideoStream()
260
 
{
261
 
        /* close each codec */
262
 
        if (ost)
263
 
        {
264
 
#if ZM_FFMPEG_SVN
265
 
                avcodec_close(ost->codec);
266
 
#else
267
 
                avcodec_close(&ost->codec);
268
 
#endif
269
 
                av_free(opicture->data[0]);
270
 
                av_free(opicture);
271
 
                if (tmp_opicture)
272
 
                {
273
 
                        av_free(tmp_opicture->data[0]);
274
 
                        av_free(tmp_opicture);
275
 
                }
276
 
                av_free(video_outbuf);
277
 
        }
278
 
 
279
 
        /* write the trailer, if any */
280
 
        av_write_trailer(ofc);
281
 
        
282
 
        /* free the streams */
283
 
        for( int i = 0; i < ofc->nb_streams; i++)
284
 
        {
285
 
                av_freep(&ofc->streams[i]);
286
 
        }
287
 
 
288
 
        if (!(of->flags & AVFMT_NOFILE))
289
 
        {
290
 
                /* close the output file */
291
 
#if ZM_FFMPEG_SVN
292
 
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(51,2,1)
293
 
                avio_close(ofc->pb);
294
 
#else
295
 
                url_fclose(ofc->pb);
296
 
#endif
297
 
#else
298
 
                url_fclose(&ofc->pb);
299
 
#endif
300
 
        }
301
 
 
302
 
        /* free the stream */
303
 
        av_free(ofc);
304
 
}
305
 
 
306
 
double VideoStream::EncodeFrame( uint8_t *buffer, int buffer_size, bool add_timestamp, unsigned int timestamp )
307
 
{
308
 
#ifdef HAVE_LIBSWSCALE
309
 
    static struct SwsContext *img_convert_ctx = 0;
310
 
#endif // HAVE_LIBSWSCALE
311
 
        double pts = 0.0;
312
 
 
313
 
 
314
 
        if (ost)
315
 
        {
316
 
#if ZM_FFMPEG_048
317
 
                pts = (double)ost->pts.val * ofc->pts_num / ofc->pts_den;
318
 
#else
319
 
                pts = (double)ost->pts.val * ost->time_base.num / ost->time_base.den;
320
 
#endif
321
 
        }
322
 
 
323
 
#if ZM_FFMPEG_SVN
324
 
        AVCodecContext *c = ost->codec;
325
 
#else
326
 
        AVCodecContext *c = &ost->codec;
327
 
#endif
328
 
        if ( c->pix_fmt != pf )
329
 
        {
330
 
                memcpy( tmp_opicture->data[0], buffer, buffer_size );
331
 
#ifdef HAVE_LIBSWSCALE
332
 
        if ( !img_convert_ctx )
333
 
        {
334
 
            img_convert_ctx = sws_getCachedContext( NULL, c->width, c->height, pf, c->width, c->height, c->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL );
335
 
            if ( !img_convert_ctx )
336
 
                Panic( "Unable to initialise image scaling context" );
337
 
        }
338
 
        sws_scale( img_convert_ctx, tmp_opicture->data, tmp_opicture->linesize, 0, c->height, opicture->data, opicture->linesize );
339
 
#else // HAVE_LIBSWSCALE
340
 
                img_convert( (AVPicture *)opicture, c->pix_fmt, (AVPicture *)tmp_opicture, pf, c->width, c->height );
341
 
#endif // HAVE_LIBSWSCALE
342
 
        }
343
 
        else
344
 
        {
345
 
                memcpy( opicture->data[0], buffer, buffer_size );
346
 
        }
347
 
        AVFrame *opicture_ptr = opicture;
348
 
 
349
 
        int ret = 0;
350
 
        if ( ofc->oformat->flags & AVFMT_RAWPICTURE )
351
 
        {
352
 
#if ZM_FFMPEG_048
353
 
                ret = av_write_frame( ofc, ost->index, (uint8_t *)opicture_ptr, sizeof(AVPicture) );
354
 
#else
355
 
                AVPacket pkt;
356
 
                av_init_packet( &pkt );
357
 
 
358
 
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(51,2,1)
359
 
                pkt.flags |= AV_PKT_FLAG_KEY;
360
 
#else
361
 
                pkt.flags |= PKT_FLAG_KEY;
362
 
#endif
363
 
                pkt.stream_index = ost->index;
364
 
                pkt.data = (uint8_t *)opicture_ptr;
365
 
                pkt.size = sizeof(AVPicture);
366
 
 
367
 
                ret = av_write_frame(ofc, &pkt);
368
 
#endif
369
 
        }
370
 
        else
371
 
        {
372
 
                if ( add_timestamp )
373
 
                        ost->pts.val = timestamp;
374
 
                int out_size = avcodec_encode_video(c, video_outbuf, video_outbuf_size, opicture_ptr);
375
 
                if ( out_size > 0 )
376
 
                {
377
 
#if ZM_FFMPEG_048
378
 
                        ret = av_write_frame(ofc, ost->index, video_outbuf, out_size);
379
 
#else
380
 
                        AVPacket pkt;
381
 
                        av_init_packet(&pkt);
382
 
 
383
 
#if ZM_FFMPEG_049
384
 
                        pkt.pts = c->coded_frame->pts;
385
 
#else
386
 
                        pkt.pts= av_rescale_q( c->coded_frame->pts, c->time_base, ost->time_base );
387
 
#endif
388
 
                        if(c->coded_frame->key_frame)
389
 
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(51,2,1)
390
 
                                pkt.flags |= AV_PKT_FLAG_KEY;
391
 
#else
392
 
                                pkt.flags |= PKT_FLAG_KEY;
393
 
#endif
394
 
                        pkt.stream_index = ost->index;
395
 
                        pkt.data = video_outbuf;
396
 
                        pkt.size = out_size;
397
 
 
398
 
                        ret = av_write_frame( ofc, &pkt );
399
 
#endif
400
 
                }
401
 
        }
402
 
        if ( ret != 0 )
403
 
        {
404
 
                Fatal( "Error %d while writing video frame: %s", ret, strerror( errno ) );
405
 
        }
406
 
        return( pts );
407
 
}
408
 
 
409
 
#endif // HAVE_LIBAVCODEC