1 /* GStreamer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <assert.h>
25 #include <string.h>
26 /* for stats file handling */
27 #include <stdio.h>
28 #include <glib/gstdio.h>
29 #include <errno.h>
30
31 #include <libavcodec/avcodec.h>
32 #include <libavutil/stereo3d.h>
33 #include <libavutil/opt.h>
34
35 #include "gstav.h"
36 #include "gstavcodecmap.h"
37 #include "gstavutils.h"
38 #include "gstavvidenc.h"
39 #include "gstavcfg.h"
40
41
42 enum
43 {
44 PROP_0,
45 PROP_QUANTIZER,
46 PROP_PASS,
47 PROP_FILENAME,
48 PROP_CFG_BASE,
49 };
50
51 static void gst_ffmpegvidenc_class_init (GstFFMpegVidEncClass * klass);
52 static void gst_ffmpegvidenc_base_init (GstFFMpegVidEncClass * klass);
53 static void gst_ffmpegvidenc_init (GstFFMpegVidEnc * ffmpegenc);
54 static void gst_ffmpegvidenc_finalize (GObject * object);
55
56 static gboolean gst_ffmpegvidenc_start (GstVideoEncoder * encoder);
57 static gboolean gst_ffmpegvidenc_stop (GstVideoEncoder * encoder);
58 static GstFlowReturn gst_ffmpegvidenc_finish (GstVideoEncoder * encoder);
59 static gboolean gst_ffmpegvidenc_set_format (GstVideoEncoder * encoder,
60 GstVideoCodecState * state);
61 static gboolean gst_ffmpegvidenc_propose_allocation (GstVideoEncoder * encoder,
62 GstQuery * query);
63 static gboolean gst_ffmpegvidenc_flush (GstVideoEncoder * encoder);
64
65 static GstFlowReturn gst_ffmpegvidenc_handle_frame (GstVideoEncoder * encoder,
66 GstVideoCodecFrame * frame);
67
68 static void gst_ffmpegvidenc_set_property (GObject * object,
69 guint prop_id, const GValue * value, GParamSpec * pspec);
70 static void gst_ffmpegvidenc_get_property (GObject * object,
71 guint prop_id, GValue * value, GParamSpec * pspec);
72
73 #define GST_FFENC_PARAMS_QDATA g_quark_from_static_string("avenc-params")
74
75 static GstElementClass *parent_class = NULL;
76
77 #define GST_TYPE_FFMPEG_PASS (gst_ffmpeg_pass_get_type ())
78 static GType
gst_ffmpeg_pass_get_type(void)79 gst_ffmpeg_pass_get_type (void)
80 {
81 static GType ffmpeg_pass_type = 0;
82
83 if (!ffmpeg_pass_type) {
84 static const GEnumValue ffmpeg_passes[] = {
85 {0, "Constant Bitrate Encoding", "cbr"},
86 {AV_CODEC_FLAG_QSCALE, "Constant Quantizer", "quant"},
87 {AV_CODEC_FLAG_PASS1, "VBR Encoding - Pass 1", "pass1"},
88 {AV_CODEC_FLAG_PASS2, "VBR Encoding - Pass 2", "pass2"},
89 {0, NULL, NULL},
90 };
91
92 ffmpeg_pass_type =
93 g_enum_register_static ("GstLibAVEncPass", ffmpeg_passes);
94 }
95
96 return ffmpeg_pass_type;
97 }
98
99 static void
gst_ffmpegvidenc_base_init(GstFFMpegVidEncClass * klass)100 gst_ffmpegvidenc_base_init (GstFFMpegVidEncClass * klass)
101 {
102 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
103 AVCodec *in_plugin;
104 GstPadTemplate *srctempl = NULL, *sinktempl = NULL;
105 GstCaps *srccaps = NULL, *sinkcaps = NULL;
106 gchar *longname, *description;
107 const gchar *classification;
108
109 in_plugin =
110 (AVCodec *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
111 GST_FFENC_PARAMS_QDATA);
112 g_assert (in_plugin != NULL);
113
114 /* construct the element details struct */
115 longname = g_strdup_printf ("libav %s encoder", in_plugin->long_name);
116 description = g_strdup_printf ("libav %s encoder", in_plugin->name);
117 classification =
118 gst_ffmpeg_codecid_is_image (in_plugin->id) ? "Codec/Encoder/Image" :
119 "Codec/Encoder/Video";
120 gst_element_class_set_metadata (element_class, longname,
121 classification, description,
122 "Wim Taymans <wim.taymans@gmail.com>, "
123 "Ronald Bultje <rbultje@ronald.bitfreak.net>");
124 g_free (longname);
125 g_free (description);
126
127 if (!(srccaps = gst_ffmpeg_codecid_to_caps (in_plugin->id, NULL, TRUE))) {
128 GST_DEBUG ("Couldn't get source caps for encoder '%s'", in_plugin->name);
129 srccaps = gst_caps_new_empty_simple ("unknown/unknown");
130 }
131
132 sinkcaps = gst_ffmpeg_codectype_to_video_caps (NULL,
133 in_plugin->id, TRUE, in_plugin);
134 if (!sinkcaps) {
135 GST_DEBUG ("Couldn't get sink caps for encoder '%s'", in_plugin->name);
136 sinkcaps = gst_caps_new_empty_simple ("unknown/unknown");
137 }
138
139 /* pad templates */
140 sinktempl = gst_pad_template_new ("sink", GST_PAD_SINK,
141 GST_PAD_ALWAYS, sinkcaps);
142 srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
143
144 gst_element_class_add_pad_template (element_class, srctempl);
145 gst_element_class_add_pad_template (element_class, sinktempl);
146
147 gst_caps_unref (sinkcaps);
148 gst_caps_unref (srccaps);
149
150 klass->in_plugin = in_plugin;
151 klass->srctempl = srctempl;
152 klass->sinktempl = sinktempl;
153
154 return;
155 }
156
157 static void
gst_ffmpegvidenc_class_init(GstFFMpegVidEncClass * klass)158 gst_ffmpegvidenc_class_init (GstFFMpegVidEncClass * klass)
159 {
160 GObjectClass *gobject_class;
161 GstVideoEncoderClass *venc_class;
162
163 gobject_class = (GObjectClass *) klass;
164 venc_class = (GstVideoEncoderClass *) klass;
165
166 parent_class = g_type_class_peek_parent (klass);
167
168 gobject_class->set_property = gst_ffmpegvidenc_set_property;
169 gobject_class->get_property = gst_ffmpegvidenc_get_property;
170
171 g_object_class_install_property (gobject_class, PROP_QUANTIZER,
172 g_param_spec_float ("quantizer", "Constant Quantizer",
173 "Constant Quantizer", 0, 30, 0.01f,
174 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT));
175
176 g_object_class_install_property (gobject_class, PROP_PASS,
177 g_param_spec_enum ("pass", "Encoding pass/type",
178 "Encoding pass/type", GST_TYPE_FFMPEG_PASS, 0,
179 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT));
180
181 g_object_class_install_property (gobject_class, PROP_FILENAME,
182 g_param_spec_string ("multipass-cache-file", "Multipass Cache File",
183 "Filename for multipass cache file", "stats.log",
184 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT));
185
186 /* register additional properties, possibly dependent on the exact CODEC */
187 gst_ffmpeg_cfg_install_properties (gobject_class, klass->in_plugin,
188 PROP_CFG_BASE, AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_VIDEO_PARAM);
189
190 venc_class->start = gst_ffmpegvidenc_start;
191 venc_class->stop = gst_ffmpegvidenc_stop;
192 venc_class->finish = gst_ffmpegvidenc_finish;
193 venc_class->handle_frame = gst_ffmpegvidenc_handle_frame;
194 venc_class->set_format = gst_ffmpegvidenc_set_format;
195 venc_class->propose_allocation = gst_ffmpegvidenc_propose_allocation;
196 venc_class->flush = gst_ffmpegvidenc_flush;
197
198 gobject_class->finalize = gst_ffmpegvidenc_finalize;
199
200 gst_type_mark_as_plugin_api (GST_TYPE_FFMPEG_PASS, 0);
201 }
202
203 static void
gst_ffmpegvidenc_init(GstFFMpegVidEnc * ffmpegenc)204 gst_ffmpegvidenc_init (GstFFMpegVidEnc * ffmpegenc)
205 {
206 GstFFMpegVidEncClass *klass =
207 (GstFFMpegVidEncClass *) G_OBJECT_GET_CLASS (ffmpegenc);
208
209 GST_PAD_SET_ACCEPT_TEMPLATE (GST_VIDEO_ENCODER_SINK_PAD (ffmpegenc));
210
211 ffmpegenc->context = avcodec_alloc_context3 (klass->in_plugin);
212 ffmpegenc->refcontext = avcodec_alloc_context3 (klass->in_plugin);
213 ffmpegenc->picture = av_frame_alloc ();
214 ffmpegenc->opened = FALSE;
215 ffmpegenc->file = NULL;
216 }
217
218 static void
gst_ffmpegvidenc_finalize(GObject * object)219 gst_ffmpegvidenc_finalize (GObject * object)
220 {
221 GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) object;
222
223 /* clean up remaining allocated data */
224 av_frame_free (&ffmpegenc->picture);
225 gst_ffmpeg_avcodec_close (ffmpegenc->context);
226 gst_ffmpeg_avcodec_close (ffmpegenc->refcontext);
227 av_freep (&ffmpegenc->context);
228 av_freep (&ffmpegenc->refcontext);
229
230 G_OBJECT_CLASS (parent_class)->finalize (object);
231 }
232
233 static gboolean
gst_ffmpegvidenc_set_format(GstVideoEncoder * encoder,GstVideoCodecState * state)234 gst_ffmpegvidenc_set_format (GstVideoEncoder * encoder,
235 GstVideoCodecState * state)
236 {
237 GstCaps *other_caps;
238 GstCaps *allowed_caps;
239 GstCaps *icaps;
240 GstVideoCodecState *output_format;
241 enum AVPixelFormat pix_fmt;
242 GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) encoder;
243 GstFFMpegVidEncClass *oclass =
244 (GstFFMpegVidEncClass *) G_OBJECT_GET_CLASS (ffmpegenc);
245
246 ffmpegenc->need_reopen = FALSE;
247
248 /* close old session */
249 if (ffmpegenc->opened) {
250 avcodec_free_context (&ffmpegenc->context);
251 ffmpegenc->opened = FALSE;
252 ffmpegenc->context = avcodec_alloc_context3 (oclass->in_plugin);
253 if (ffmpegenc->context == NULL) {
254 GST_DEBUG_OBJECT (ffmpegenc, "Failed to set context defaults");
255 return FALSE;
256 }
257 }
258
259 /* additional avcodec settings */
260 gst_ffmpeg_cfg_fill_context (G_OBJECT (ffmpegenc), ffmpegenc->context);
261
262 if (GST_VIDEO_INFO_IS_INTERLACED (&state->info))
263 ffmpegenc->context->flags |=
264 AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME;
265
266 /* and last but not least the pass; CBR, 2-pass, etc */
267 ffmpegenc->context->flags |= ffmpegenc->pass;
268 switch (ffmpegenc->pass) {
269 /* some additional action depends on type of pass */
270 case AV_CODEC_FLAG_QSCALE:
271 ffmpegenc->context->global_quality
272 = ffmpegenc->picture->quality = FF_QP2LAMBDA * ffmpegenc->quantizer;
273 break;
274 case AV_CODEC_FLAG_PASS1: /* need to prepare a stats file */
275 /* we don't close when changing caps, fingers crossed */
276 if (!ffmpegenc->file)
277 ffmpegenc->file = g_fopen (ffmpegenc->filename, "w");
278 if (!ffmpegenc->file)
279 goto open_file_err;
280 break;
281 case AV_CODEC_FLAG_PASS2:
282 { /* need to read the whole stats file ! */
283 gsize size;
284
285 if (!g_file_get_contents (ffmpegenc->filename,
286 &ffmpegenc->context->stats_in, &size, NULL))
287 goto file_read_err;
288
289 break;
290 }
291 default:
292 break;
293 }
294
295 GST_DEBUG_OBJECT (ffmpegenc, "Extracting common video information");
296 /* fetch pix_fmt, fps, par, width, height... */
297 gst_ffmpeg_videoinfo_to_context (&state->info, ffmpegenc->context);
298
299 /* sanitize time base */
300 if (ffmpegenc->context->time_base.num <= 0
301 || ffmpegenc->context->time_base.den <= 0)
302 goto insane_timebase;
303
304 if ((oclass->in_plugin->id == AV_CODEC_ID_MPEG4)
305 && (ffmpegenc->context->time_base.den > 65535)) {
306 /* MPEG4 Standards do not support time_base denominator greater than
307 * (1<<16) - 1 . We therefore scale them down.
308 * Agreed, it will not be the exact framerate... but the difference
309 * shouldn't be that noticeable */
310 ffmpegenc->context->time_base.num =
311 (gint) gst_util_uint64_scale_int (ffmpegenc->context->time_base.num,
312 65535, ffmpegenc->context->time_base.den);
313 ffmpegenc->context->time_base.den = 65535;
314 GST_LOG_OBJECT (ffmpegenc, "MPEG4 : scaled down framerate to %d / %d",
315 ffmpegenc->context->time_base.den, ffmpegenc->context->time_base.num);
316 }
317
318 pix_fmt = ffmpegenc->context->pix_fmt;
319
320 /* some codecs support more than one format, first auto-choose one */
321 GST_DEBUG_OBJECT (ffmpegenc, "picking an output format ...");
322 allowed_caps = gst_pad_get_allowed_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
323 if (!allowed_caps) {
324 GST_DEBUG_OBJECT (ffmpegenc, "... but no peer, using template caps");
325 /* we need to copy because get_allowed_caps returns a ref, and
326 * get_pad_template_caps doesn't */
327 allowed_caps =
328 gst_pad_get_pad_template_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
329 }
330 GST_DEBUG_OBJECT (ffmpegenc, "chose caps %" GST_PTR_FORMAT, allowed_caps);
331 gst_ffmpeg_caps_with_codecid (oclass->in_plugin->id,
332 oclass->in_plugin->type, allowed_caps, ffmpegenc->context);
333
334 /* open codec */
335 if (gst_ffmpeg_avcodec_open (ffmpegenc->context, oclass->in_plugin) < 0) {
336 gst_caps_unref (allowed_caps);
337 goto open_codec_fail;
338 }
339
340 /* is the colourspace correct? */
341 if (pix_fmt != ffmpegenc->context->pix_fmt) {
342 gst_caps_unref (allowed_caps);
343 goto pix_fmt_err;
344 }
345
346 /* we may have failed mapping caps to a pixfmt,
347 * and quite some codecs do not make up their own mind about that
348 * in any case, _NONE can never work out later on */
349 if (pix_fmt == AV_PIX_FMT_NONE) {
350 gst_caps_unref (allowed_caps);
351 goto bad_input_fmt;
352 }
353
354 /* second pass stats buffer no longer needed */
355 g_free (ffmpegenc->context->stats_in);
356
357 /* try to set this caps on the other side */
358 other_caps = gst_ffmpeg_codecid_to_caps (oclass->in_plugin->id,
359 ffmpegenc->context, TRUE);
360
361 if (!other_caps) {
362 gst_caps_unref (allowed_caps);
363 goto unsupported_codec;
364 }
365
366 icaps = gst_caps_intersect (allowed_caps, other_caps);
367 gst_caps_unref (allowed_caps);
368 gst_caps_unref (other_caps);
369 if (gst_caps_is_empty (icaps)) {
370 gst_caps_unref (icaps);
371 goto unsupported_codec;
372 }
373 icaps = gst_caps_fixate (icaps);
374
375 GST_DEBUG_OBJECT (ffmpegenc, "codec flags 0x%08x", ffmpegenc->context->flags);
376
377 /* Store input state and set output state */
378 if (ffmpegenc->input_state)
379 gst_video_codec_state_unref (ffmpegenc->input_state);
380 ffmpegenc->input_state = gst_video_codec_state_ref (state);
381
382 output_format = gst_video_encoder_set_output_state (encoder, icaps, state);
383 gst_video_codec_state_unref (output_format);
384
385 /* Store some tags */
386 {
387 GstTagList *tags = gst_tag_list_new_empty ();
388 const gchar *codec;
389
390 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_NOMINAL_BITRATE,
391 (guint) ffmpegenc->context->bit_rate, NULL);
392
393 if ((codec =
394 gst_ffmpeg_get_codecid_longname (ffmpegenc->context->codec_id)))
395 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_VIDEO_CODEC, codec,
396 NULL);
397
398 gst_video_encoder_merge_tags (encoder, tags, GST_TAG_MERGE_REPLACE);
399 gst_tag_list_unref (tags);
400 }
401
402 /* success! */
403 ffmpegenc->opened = TRUE;
404
405 return TRUE;
406
407 /* ERRORS */
408 open_file_err:
409 {
410 GST_ELEMENT_ERROR (ffmpegenc, RESOURCE, OPEN_WRITE,
411 (("Could not open file \"%s\" for writing."), ffmpegenc->filename),
412 GST_ERROR_SYSTEM);
413 return FALSE;
414 }
415 file_read_err:
416 {
417 GST_ELEMENT_ERROR (ffmpegenc, RESOURCE, READ,
418 (("Could not get contents of file \"%s\"."), ffmpegenc->filename),
419 GST_ERROR_SYSTEM);
420 return FALSE;
421 }
422
423 insane_timebase:
424 {
425 GST_ERROR_OBJECT (ffmpegenc, "Rejecting time base %d/%d",
426 ffmpegenc->context->time_base.den, ffmpegenc->context->time_base.num);
427 goto cleanup_stats_in;
428 }
429 unsupported_codec:
430 {
431 GST_DEBUG ("Unsupported codec - no caps found");
432 goto cleanup_stats_in;
433 }
434 open_codec_fail:
435 {
436 GST_DEBUG_OBJECT (ffmpegenc, "avenc_%s: Failed to open libav codec",
437 oclass->in_plugin->name);
438 goto close_codec;
439 }
440
441 pix_fmt_err:
442 {
443 GST_DEBUG_OBJECT (ffmpegenc,
444 "avenc_%s: AV wants different colourspace (%d given, %d wanted)",
445 oclass->in_plugin->name, pix_fmt, ffmpegenc->context->pix_fmt);
446 goto close_codec;
447 }
448
449 bad_input_fmt:
450 {
451 GST_DEBUG_OBJECT (ffmpegenc, "avenc_%s: Failed to determine input format",
452 oclass->in_plugin->name);
453 goto close_codec;
454 }
455 close_codec:
456 {
457 avcodec_free_context (&ffmpegenc->context);
458 ffmpegenc->context = avcodec_alloc_context3 (oclass->in_plugin);
459 if (ffmpegenc->context == NULL)
460 GST_DEBUG_OBJECT (ffmpegenc, "Failed to set context defaults");
461 goto cleanup_stats_in;
462 }
463 cleanup_stats_in:
464 {
465 g_free (ffmpegenc->context->stats_in);
466 return FALSE;
467 }
468 }
469
470
471 static gboolean
gst_ffmpegvidenc_propose_allocation(GstVideoEncoder * encoder,GstQuery * query)472 gst_ffmpegvidenc_propose_allocation (GstVideoEncoder * encoder,
473 GstQuery * query)
474 {
475 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
476
477 return GST_VIDEO_ENCODER_CLASS (parent_class)->propose_allocation (encoder,
478 query);
479 }
480
481 static void
gst_ffmpegvidenc_free_avpacket(gpointer pkt)482 gst_ffmpegvidenc_free_avpacket (gpointer pkt)
483 {
484 av_packet_unref ((AVPacket *) pkt);
485 g_slice_free (AVPacket, pkt);
486 }
487
488 typedef struct
489 {
490 GstBuffer *buffer;
491 GstVideoFrame vframe;
492 } BufferInfo;
493
494 static void
buffer_info_free(void * opaque,guint8 * data)495 buffer_info_free (void *opaque, guint8 * data)
496 {
497 BufferInfo *info = opaque;
498
499 gst_video_frame_unmap (&info->vframe);
500 gst_buffer_unref (info->buffer);
501 g_slice_free (BufferInfo, info);
502 }
503
504 static enum AVStereo3DType
stereo_gst_to_av(GstVideoMultiviewMode mview_mode)505 stereo_gst_to_av (GstVideoMultiviewMode mview_mode)
506 {
507 switch (mview_mode) {
508 case GST_VIDEO_MULTIVIEW_MODE_MONO:
509 /* Video is not stereoscopic (and metadata has to be there). */
510 return AV_STEREO3D_2D;
511 case GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE:
512 return AV_STEREO3D_SIDEBYSIDE;
513 case GST_VIDEO_MULTIVIEW_MODE_TOP_BOTTOM:
514 return AV_STEREO3D_TOPBOTTOM;
515 case GST_VIDEO_MULTIVIEW_MODE_FRAME_BY_FRAME:
516 return AV_STEREO3D_FRAMESEQUENCE;
517 case GST_VIDEO_MULTIVIEW_MODE_CHECKERBOARD:
518 return AV_STEREO3D_CHECKERBOARD;
519 case GST_VIDEO_MULTIVIEW_MODE_SIDE_BY_SIDE_QUINCUNX:
520 return AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
521 case GST_VIDEO_MULTIVIEW_MODE_ROW_INTERLEAVED:
522 return AV_STEREO3D_LINES;
523 case GST_VIDEO_MULTIVIEW_MODE_COLUMN_INTERLEAVED:
524 return AV_STEREO3D_COLUMNS;
525 default:
526 break;
527 }
528 GST_WARNING ("Unsupported multiview mode - no mapping in libav");
529 return AV_STEREO3D_2D;
530 }
531
532 static void
gst_ffmpegvidenc_add_cc(GstBuffer * buffer,AVFrame * picture)533 gst_ffmpegvidenc_add_cc (GstBuffer * buffer, AVFrame * picture)
534 {
535 GstVideoCaptionMeta *cc_meta;
536 gpointer iter = NULL;
537
538 while ((cc_meta =
539 (GstVideoCaptionMeta *) gst_buffer_iterate_meta_filtered (buffer,
540 &iter, GST_VIDEO_CAPTION_META_API_TYPE))) {
541 AVFrameSideData *sd;
542
543 if (cc_meta->caption_type != GST_VIDEO_CAPTION_TYPE_CEA708_RAW)
544 continue;
545
546 sd = av_frame_new_side_data (picture, AV_FRAME_DATA_A53_CC, cc_meta->size);
547 memcpy (sd->data, cc_meta->data, cc_meta->size);
548 }
549 }
550
551 static GstFlowReturn
gst_ffmpegvidenc_send_frame(GstFFMpegVidEnc * ffmpegenc,GstVideoCodecFrame * frame)552 gst_ffmpegvidenc_send_frame (GstFFMpegVidEnc * ffmpegenc,
553 GstVideoCodecFrame * frame)
554 {
555 GstVideoInfo *info = &ffmpegenc->input_state->info;
556 BufferInfo *buffer_info;
557 guint c;
558 gint res;
559 GstFlowReturn ret = GST_FLOW_ERROR;
560 AVFrame *picture = NULL;
561
562 if (!frame)
563 goto send_frame;
564
565 picture = ffmpegenc->picture;
566
567 gst_ffmpegvidenc_add_cc (frame->input_buffer, picture);
568
569 if (GST_VIDEO_INFO_IS_INTERLACED (&ffmpegenc->input_state->info)) {
570 picture->interlaced_frame = TRUE;
571 picture->top_field_first =
572 GST_BUFFER_FLAG_IS_SET (frame->input_buffer, GST_VIDEO_BUFFER_FLAG_TFF)
573 || GST_VIDEO_INFO_FIELD_ORDER (&ffmpegenc->input_state->info) ==
574 GST_VIDEO_FIELD_ORDER_TOP_FIELD_FIRST;
575 picture->repeat_pict =
576 GST_BUFFER_FLAG_IS_SET (frame->input_buffer, GST_VIDEO_BUFFER_FLAG_RFF);
577 }
578
579 if (GST_VIDEO_INFO_MULTIVIEW_MODE (info) != GST_VIDEO_MULTIVIEW_MODE_NONE) {
580 AVStereo3D *stereo = av_stereo3d_create_side_data (picture);
581 stereo->type = stereo_gst_to_av (GST_VIDEO_INFO_MULTIVIEW_MODE (info));
582
583 if (GST_VIDEO_INFO_MULTIVIEW_FLAGS (info) &
584 GST_VIDEO_MULTIVIEW_FLAGS_RIGHT_VIEW_FIRST) {
585 stereo->flags = AV_STEREO3D_FLAG_INVERT;
586 }
587 }
588
589 if (GST_VIDEO_CODEC_FRAME_IS_FORCE_KEYFRAME (frame))
590 picture->pict_type = AV_PICTURE_TYPE_I;
591
592 buffer_info = g_slice_new0 (BufferInfo);
593 buffer_info->buffer = gst_buffer_ref (frame->input_buffer);
594
595 if (!gst_video_frame_map (&buffer_info->vframe, info, frame->input_buffer,
596 GST_MAP_READ)) {
597 GST_ERROR_OBJECT (ffmpegenc, "Failed to map input buffer");
598 gst_buffer_unref (buffer_info->buffer);
599 g_slice_free (BufferInfo, buffer_info);
600 gst_video_codec_frame_unref (frame);
601 goto done;
602 }
603
604 /* Fill avpicture */
605 picture->buf[0] =
606 av_buffer_create (NULL, 0, buffer_info_free, buffer_info, 0);
607 for (c = 0; c < AV_NUM_DATA_POINTERS; c++) {
608 if (c < GST_VIDEO_INFO_N_COMPONENTS (info)) {
609 picture->data[c] = GST_VIDEO_FRAME_PLANE_DATA (&buffer_info->vframe, c);
610 picture->linesize[c] =
611 GST_VIDEO_FRAME_COMP_STRIDE (&buffer_info->vframe, c);
612 } else {
613 picture->data[c] = NULL;
614 picture->linesize[c] = 0;
615 }
616 }
617
618 picture->format = ffmpegenc->context->pix_fmt;
619 picture->width = GST_VIDEO_FRAME_WIDTH (&buffer_info->vframe);
620 picture->height = GST_VIDEO_FRAME_HEIGHT (&buffer_info->vframe);
621
622 picture->pts =
623 gst_ffmpeg_time_gst_to_ff (frame->pts /
624 ffmpegenc->context->ticks_per_frame, ffmpegenc->context->time_base);
625
626 send_frame:
627 if (!picture) {
628 GstFFMpegVidEncClass *oclass =
629 (GstFFMpegVidEncClass *) (G_OBJECT_GET_CLASS (ffmpegenc));
630
631 /* If AV_CODEC_CAP_ENCODER_FLUSH wasn't set, we need to re-open
632 * encoder */
633 if (!(oclass->in_plugin->capabilities & AV_CODEC_CAP_ENCODER_FLUSH)) {
634 GST_DEBUG_OBJECT (ffmpegenc, "Encoder needs reopen later");
635
636 /* we will reopen later handle_frame() */
637 ffmpegenc->need_reopen = TRUE;
638 }
639 }
640
641 res = avcodec_send_frame (ffmpegenc->context, picture);
642
643 if (picture)
644 av_frame_unref (picture);
645
646 if (res == 0)
647 ret = GST_FLOW_OK;
648 else if (res == AVERROR_EOF)
649 ret = GST_FLOW_EOS;
650
651 done:
652 return ret;
653 }
654
655 static GstFlowReturn
gst_ffmpegvidenc_receive_packet(GstFFMpegVidEnc * ffmpegenc,gboolean * got_packet,gboolean send)656 gst_ffmpegvidenc_receive_packet (GstFFMpegVidEnc * ffmpegenc,
657 gboolean * got_packet, gboolean send)
658 {
659 AVPacket *pkt;
660 GstBuffer *outbuf;
661 GstVideoCodecFrame *frame;
662 gint res;
663 GstFlowReturn ret = GST_FLOW_OK;
664
665 *got_packet = FALSE;
666
667 pkt = g_slice_new0 (AVPacket);
668
669 res = avcodec_receive_packet (ffmpegenc->context, pkt);
670
671 if (res == AVERROR (EAGAIN)) {
672 g_slice_free (AVPacket, pkt);
673 goto done;
674 } else if (res == AVERROR_EOF) {
675 ret = GST_FLOW_EOS;
676 goto done;
677 } else if (res < 0) {
678 ret = GST_FLOW_ERROR;
679 goto done;
680 }
681
682 *got_packet = TRUE;
683
684 /* save stats info if there is some as well as a stats file */
685 if (ffmpegenc->file && ffmpegenc->context->stats_out)
686 if (fprintf (ffmpegenc->file, "%s", ffmpegenc->context->stats_out) < 0)
687 GST_ELEMENT_ERROR (ffmpegenc, RESOURCE, WRITE,
688 (("Could not write to file \"%s\"."), ffmpegenc->filename),
689 GST_ERROR_SYSTEM);
690
691 /* Get oldest frame */
692 frame = gst_video_encoder_get_oldest_frame (GST_VIDEO_ENCODER (ffmpegenc));
693
694 if (send) {
695 outbuf =
696 gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, pkt->data,
697 pkt->size, 0, pkt->size, pkt, gst_ffmpegvidenc_free_avpacket);
698 frame->output_buffer = outbuf;
699
700 if (pkt->flags & AV_PKT_FLAG_KEY)
701 GST_VIDEO_CODEC_FRAME_SET_SYNC_POINT (frame);
702 else
703 GST_VIDEO_CODEC_FRAME_UNSET_SYNC_POINT (frame);
704 }
705
706 frame->dts =
707 gst_ffmpeg_time_ff_to_gst (pkt->dts, ffmpegenc->context->time_base);
708 /* This will lose some precision compared to setting the PTS from the input
709 * buffer directly, but that way we're sure PTS and DTS are consistent, in
710 * particular DTS should always be <= PTS
711 */
712 frame->pts =
713 gst_ffmpeg_time_ff_to_gst (pkt->pts, ffmpegenc->context->time_base);
714
715 ret = gst_video_encoder_finish_frame (GST_VIDEO_ENCODER (ffmpegenc), frame);
716
717 done:
718 return ret;
719 }
720
721 static GstFlowReturn
gst_ffmpegvidenc_handle_frame(GstVideoEncoder * encoder,GstVideoCodecFrame * frame)722 gst_ffmpegvidenc_handle_frame (GstVideoEncoder * encoder,
723 GstVideoCodecFrame * frame)
724 {
725 GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) encoder;
726 GstFlowReturn ret;
727 gboolean got_packet;
728
729 /* endoder was drained or flushed, and ffmpeg encoder doesn't support
730 * flushing. We need to re-open encoder then */
731 if (ffmpegenc->need_reopen) {
732 gboolean reopen_ret;
733 GstVideoCodecState *input_state;
734
735 GST_DEBUG_OBJECT (ffmpegenc, "Open encoder again");
736
737 if (!ffmpegenc->input_state) {
738 GST_ERROR_OBJECT (ffmpegenc,
739 "Cannot re-open encoder without input state");
740 return GST_FLOW_NOT_NEGOTIATED;
741 }
742
743 input_state = gst_video_codec_state_ref (ffmpegenc->input_state);
744 reopen_ret = gst_ffmpegvidenc_set_format (encoder, input_state);
745 gst_video_codec_state_unref (input_state);
746
747 if (!reopen_ret) {
748 GST_ERROR_OBJECT (ffmpegenc, "Couldn't re-open encoder");
749 return GST_FLOW_NOT_NEGOTIATED;
750 }
751 }
752
753 ret = gst_ffmpegvidenc_send_frame (ffmpegenc, frame);
754
755 if (ret != GST_FLOW_OK)
756 goto encode_fail;
757
758 gst_video_codec_frame_unref (frame);
759
760 do {
761 ret = gst_ffmpegvidenc_receive_packet (ffmpegenc, &got_packet, TRUE);
762 if (ret != GST_FLOW_OK)
763 break;
764 } while (got_packet);
765
766 done:
767 return ret;
768
769 /* We choose to be error-resilient */
770 encode_fail:
771 {
772 #ifndef GST_DISABLE_GST_DEBUG
773 GstFFMpegVidEncClass *oclass =
774 (GstFFMpegVidEncClass *) (G_OBJECT_GET_CLASS (ffmpegenc));
775 GST_ERROR_OBJECT (ffmpegenc,
776 "avenc_%s: failed to encode buffer", oclass->in_plugin->name);
777 #endif /* GST_DISABLE_GST_DEBUG */
778 /* avoid frame (and ts etc) piling up */
779 ret = gst_video_encoder_finish_frame (encoder, frame);
780 goto done;
781 }
782 }
783
784 static GstFlowReturn
gst_ffmpegvidenc_flush_buffers(GstFFMpegVidEnc * ffmpegenc,gboolean send)785 gst_ffmpegvidenc_flush_buffers (GstFFMpegVidEnc * ffmpegenc, gboolean send)
786 {
787 GstFlowReturn ret = GST_FLOW_OK;
788 gboolean got_packet;
789
790 GST_DEBUG_OBJECT (ffmpegenc, "flushing buffers with sending %d", send);
791
792 /* no need to empty codec if there is none */
793 if (!ffmpegenc->opened)
794 goto done;
795
796 ret = gst_ffmpegvidenc_send_frame (ffmpegenc, NULL);
797
798 if (ret != GST_FLOW_OK)
799 goto done;
800
801 do {
802 ret = gst_ffmpegvidenc_receive_packet (ffmpegenc, &got_packet, send);
803 if (ret != GST_FLOW_OK)
804 break;
805 } while (got_packet);
806 avcodec_flush_buffers (ffmpegenc->context);
807
808 done:
809 /* FFMpeg will return AVERROR_EOF if it's internal was fully drained
810 * then we are translating it to GST_FLOW_EOS. However, because this behavior
811 * is fully internal stuff of this implementation and gstvideoencoder
812 * baseclass doesn't convert this GST_FLOW_EOS to GST_FLOW_OK,
813 * convert this flow returned here */
814 if (ret == GST_FLOW_EOS)
815 ret = GST_FLOW_OK;
816
817 return ret;
818 }
819
820 static void
gst_ffmpegvidenc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)821 gst_ffmpegvidenc_set_property (GObject * object,
822 guint prop_id, const GValue * value, GParamSpec * pspec)
823 {
824 GstFFMpegVidEnc *ffmpegenc;
825
826 ffmpegenc = (GstFFMpegVidEnc *) (object);
827
828 if (ffmpegenc->opened) {
829 GST_WARNING_OBJECT (ffmpegenc,
830 "Can't change properties once decoder is setup !");
831 return;
832 }
833
834 switch (prop_id) {
835 case PROP_QUANTIZER:
836 ffmpegenc->quantizer = g_value_get_float (value);
837 break;
838 case PROP_PASS:
839 ffmpegenc->pass = g_value_get_enum (value);
840 break;
841 case PROP_FILENAME:
842 g_free (ffmpegenc->filename);
843 ffmpegenc->filename = g_value_dup_string (value);
844 break;
845 default:
846 if (!gst_ffmpeg_cfg_set_property (ffmpegenc->refcontext, value, pspec))
847 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
848 break;
849 }
850 }
851
852 static void
gst_ffmpegvidenc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)853 gst_ffmpegvidenc_get_property (GObject * object,
854 guint prop_id, GValue * value, GParamSpec * pspec)
855 {
856 GstFFMpegVidEnc *ffmpegenc;
857
858 ffmpegenc = (GstFFMpegVidEnc *) (object);
859
860 switch (prop_id) {
861 case PROP_QUANTIZER:
862 g_value_set_float (value, ffmpegenc->quantizer);
863 break;
864 case PROP_PASS:
865 g_value_set_enum (value, ffmpegenc->pass);
866 break;
867 case PROP_FILENAME:
868 g_value_take_string (value, g_strdup (ffmpegenc->filename));
869 break;
870 default:
871 if (!gst_ffmpeg_cfg_get_property (ffmpegenc->refcontext, value, pspec))
872 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
873 break;
874 }
875 }
876
877 static gboolean
gst_ffmpegvidenc_flush(GstVideoEncoder * encoder)878 gst_ffmpegvidenc_flush (GstVideoEncoder * encoder)
879 {
880 GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) encoder;
881
882 /**
883 * ohos.opt.compat.0031
884 * Fix flush at eos and flush opearte
885 * When flush we close and open new encoder with time 5ms.
886 */
887 #ifdef OHOS_OPT_COMPAT
888 if (ffmpegenc->opened) {
889 GstFFMpegVidEncClass *oclass = (GstFFMpegVidEncClass *) G_OBJECT_GET_CLASS (ffmpegenc);
890 if (oclass->in_plugin->capabilities & AV_CODEC_CAP_ENCODER_FLUSH) {
891 avcodec_flush_buffers (ffmpegenc->context);
892 } else {
893 GstVideoCodecState *state = gst_video_codec_state_ref (ffmpegenc->input_state);
894 (void)gst_ffmpegvidenc_set_format (encoder, state);
895 gst_video_codec_state_unref (state);
896 }
897 }
898 #else
899 if (ffmpegenc->opened)
900 avcodec_flush_buffers (ffmpegenc->context);
901 #endif
902
903 return TRUE;
904 }
905
906 static gboolean
gst_ffmpegvidenc_start(GstVideoEncoder * encoder)907 gst_ffmpegvidenc_start (GstVideoEncoder * encoder)
908 {
909 GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) encoder;
910 GstFFMpegVidEncClass *oclass =
911 (GstFFMpegVidEncClass *) G_OBJECT_GET_CLASS (ffmpegenc);
912
913 ffmpegenc->opened = FALSE;
914 ffmpegenc->need_reopen = FALSE;
915
916 /* close old session */
917 avcodec_free_context (&ffmpegenc->context);
918 ffmpegenc->context = avcodec_alloc_context3 (oclass->in_plugin);
919 if (ffmpegenc->context == NULL) {
920 GST_DEBUG_OBJECT (ffmpegenc, "Failed to set context defaults");
921 return FALSE;
922 }
923
924 gst_video_encoder_set_min_pts (encoder, GST_SECOND * 60 * 60 * 1000);
925
926 return TRUE;
927 }
928
929 static gboolean
gst_ffmpegvidenc_stop(GstVideoEncoder * encoder)930 gst_ffmpegvidenc_stop (GstVideoEncoder * encoder)
931 {
932 GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) encoder;
933
934 gst_ffmpegvidenc_flush_buffers (ffmpegenc, FALSE);
935 gst_ffmpeg_avcodec_close (ffmpegenc->context);
936 ffmpegenc->opened = FALSE;
937 ffmpegenc->need_reopen = FALSE;
938
939 if (ffmpegenc->input_state) {
940 gst_video_codec_state_unref (ffmpegenc->input_state);
941 ffmpegenc->input_state = NULL;
942 }
943
944 return TRUE;
945 }
946
947 static GstFlowReturn
gst_ffmpegvidenc_finish(GstVideoEncoder * encoder)948 gst_ffmpegvidenc_finish (GstVideoEncoder * encoder)
949 {
950 GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) encoder;
951
952 return gst_ffmpegvidenc_flush_buffers (ffmpegenc, TRUE);
953 }
954
955 gboolean
gst_ffmpegvidenc_register(GstPlugin * plugin)956 gst_ffmpegvidenc_register (GstPlugin * plugin)
957 {
958 GTypeInfo typeinfo = {
959 sizeof (GstFFMpegVidEncClass),
960 (GBaseInitFunc) gst_ffmpegvidenc_base_init,
961 NULL,
962 (GClassInitFunc) gst_ffmpegvidenc_class_init,
963 NULL,
964 NULL,
965 sizeof (GstFFMpegVidEnc),
966 0,
967 (GInstanceInitFunc) gst_ffmpegvidenc_init,
968 };
969 GType type;
970 AVCodec *in_plugin;
971 void *i = 0;
972
973 GST_LOG ("Registering encoders");
974
975 while ((in_plugin = (AVCodec *) av_codec_iterate (&i))) {
976 gchar *type_name;
977
978 /* Skip non-AV codecs */
979 if (in_plugin->type != AVMEDIA_TYPE_VIDEO)
980 continue;
981
982 /* no quasi codecs, please */
983 if (in_plugin->id == AV_CODEC_ID_RAWVIDEO ||
984 in_plugin->id == AV_CODEC_ID_V210 ||
985 in_plugin->id == AV_CODEC_ID_V210X ||
986 in_plugin->id == AV_CODEC_ID_V308 ||
987 in_plugin->id == AV_CODEC_ID_V408 ||
988 in_plugin->id == AV_CODEC_ID_V410 ||
989 in_plugin->id == AV_CODEC_ID_R210
990 || in_plugin->id == AV_CODEC_ID_AYUV
991 || in_plugin->id == AV_CODEC_ID_Y41P
992 || in_plugin->id == AV_CODEC_ID_012V
993 || in_plugin->id == AV_CODEC_ID_YUV4
994 #if AV_VERSION_INT (LIBAVCODEC_VERSION_MAJOR, LIBAVCODEC_VERSION_MINOR, LIBAVCODEC_VERSION_MICRO) >= \
995 AV_VERSION_INT (57,4,0)
996 || in_plugin->id == AV_CODEC_ID_WRAPPED_AVFRAME
997 #endif
998 || in_plugin->id == AV_CODEC_ID_ZLIB) {
999 continue;
1000 }
1001
1002 /* No encoders depending on external libraries (we don't build them, but
1003 * people who build against an external ffmpeg might have them.
1004 * We have native gstreamer plugins for all of those libraries anyway. */
1005 if (!strncmp (in_plugin->name, "lib", 3)) {
1006 GST_DEBUG
1007 ("Not using external library encoder %s. Use the gstreamer-native ones instead.",
1008 in_plugin->name);
1009 continue;
1010 }
1011
1012 /* Skip hardware or hybrid (hardware with software fallback) */
1013 if ((in_plugin->capabilities & AV_CODEC_CAP_HARDWARE) ==
1014 AV_CODEC_CAP_HARDWARE) {
1015 GST_DEBUG
1016 ("Ignoring hardware encoder %s. We can't handle this outside of ffmpeg",
1017 in_plugin->name);
1018 continue;
1019 }
1020
1021 if ((in_plugin->capabilities & AV_CODEC_CAP_HYBRID) == AV_CODEC_CAP_HYBRID) {
1022 GST_DEBUG
1023 ("Ignoring hybrid encoder %s. We can't handle this outside of ffmpeg",
1024 in_plugin->name);
1025 continue;
1026 }
1027
1028 /* only video encoders */
1029 if (!av_codec_is_encoder (in_plugin)
1030 || in_plugin->type != AVMEDIA_TYPE_VIDEO)
1031 continue;
1032
1033 /* FIXME : We should have a method to know cheaply whether we have a mapping
1034 * for the given plugin or not */
1035
1036 GST_DEBUG ("Trying plugin %s [%s]", in_plugin->name, in_plugin->long_name);
1037
1038 /* no codecs for which we're GUARANTEED to have better alternatives */
1039 if (!strcmp (in_plugin->name, "gif")) {
1040 GST_LOG ("Ignoring encoder %s", in_plugin->name);
1041 continue;
1042 }
1043
1044 /* construct the type */
1045 type_name = g_strdup_printf ("avenc_%s", in_plugin->name);
1046
1047 type = g_type_from_name (type_name);
1048
1049 if (!type) {
1050
1051 /* create the glib type now */
1052 type =
1053 g_type_register_static (GST_TYPE_VIDEO_ENCODER, type_name, &typeinfo,
1054 0);
1055 g_type_set_qdata (type, GST_FFENC_PARAMS_QDATA, (gpointer) in_plugin);
1056
1057 {
1058 static const GInterfaceInfo preset_info = {
1059 NULL,
1060 NULL,
1061 NULL
1062 };
1063 g_type_add_interface_static (type, GST_TYPE_PRESET, &preset_info);
1064 }
1065 }
1066
1067 if (!gst_element_register (plugin, type_name, GST_RANK_SECONDARY, type)) {
1068 g_free (type_name);
1069 return FALSE;
1070 }
1071
1072 g_free (type_name);
1073 }
1074
1075 GST_LOG ("Finished registering encoders");
1076
1077 return TRUE;
1078 }
1079