ff-save.c 22.1 KB
Newer Older
1 2 3 4 5
/* This file is an image processing operation for GEGL
 *
 * GEGL is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
6
 * version 3 of the License, or (at your option) any later version.
7 8 9 10 11 12 13
 *
 * GEGL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
14
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
15 16 17
 *
 * Copyright 2003,2004,2007 Øyvind Kolås <pippin@gimp.org>
 */
18 19

#include "config.h"
Martin Nordholts's avatar
Martin Nordholts committed
20 21 22

#include <stdlib.h>

23 24
#include <glib/gi18n-lib.h>

25 26
#include <libswscale/swscale.h>

27

28
#ifdef GEGL_PROPERTIES
29

30 31 32
property_string (path, _("File"), "/tmp/fnord.mp4")
    description (_("Target path and filename, use '-' for stdout."))

33
property_double (bitrate, _("Target bitrate"), 800000.0)
34 35
     value_range (0.0, 100000000.0)

36
property_double (fps, _("Frames/second"), 25)
37
     value_range (0.0, 100.0)
38 39 40

#else

41
#define GEGL_OP_SINK
42
#define GEGL_OP_C_SOURCE ff-save.c
43

44 45 46
#define FF_API_OLD_ENCODE_AUDIO 1
#define FF_API_DUMP_FORMAT  1

47
#include "gegl-op.h"
48

49
#include <libavformat/avformat.h>
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

typedef struct
{
  gdouble    frame;
  gdouble    frames;
  gdouble    width;
  gdouble    height;
  GeglBuffer *input;

  AVOutputFormat *fmt;
  AVFormatContext *oc;
  AVStream *video_st;

  AVFrame  *picture, *tmp_picture;
  uint8_t  *video_outbuf;
  int       frame_count, video_outbuf_size;

67 68 69 70
    /** the rest is for audio handling within oxide, note that the interface
     * used passes all used functions in the oxide api through the reg_sym api
     * of gggl, this means that the ops should be usable by other applications
     * using gggl directly,. without needing to link with the oxide library
71 72 73 74
     */

  AVStream *audio_st;

Kevin Cozens's avatar
Kevin Cozens committed
75
  void     *oxide_audio_instance;
76
  /*< non NULL audio_query,. means audio present */
77

78 79 80 81 82 83
  int32_t (*oxide_audio_query) (void *audio_instance,
                                uint32_t * sample_rate,
                                uint32_t * bits,
                                uint32_t * channels,
                                uint32_t * fragment_samples,
                                uint32_t * fragment_size);
84

85 86 87
  /* get audio samples for the current video frame, this should provide all
   * audiosamples associated with the frame, frame centering on audio stream is
   * undefined (FIXME:<<)
88 89
   */

90
  int32_t (*oxide_audio_get_fragment) (void *audio_instance, uint8_t * buf);
91 92 93 94 95 96 97 98 99 100 101 102

  uint32_t  sample_rate;
  uint32_t  bits;
  uint32_t  channels;
  uint32_t  fragment_samples;
  uint32_t  fragment_size;

  uint8_t  *fragment;

  int       buffer_size;
  int       buffer_read_pos;
  int       buffer_write_pos;
103 104 105 106
  uint8_t  *buffer;   /* optimal buffer size should be calculated,. preferably
                         run time,. no magic numbers needed for the filewrite
                         case, perhaps a tiny margin for ntsc since it has a
                         strange framerate */
107 108 109 110 111 112 113 114

  int       audio_outbuf_size;
  int       audio_input_frame_size;
  int16_t  *samples;
  uint8_t  *audio_outbuf;
} Priv;

#define DISABLE_AUDIO
115

Kevin Cozens's avatar
Kevin Cozens committed
116
static void
117
init (GeglProperties *o)
118
{
Kevin Cozens's avatar
Kevin Cozens committed
119
  static gint inited = 0; /*< this is actually meant to be static, only to be done once */
120
  Priv       *p = (Priv*)o->user_data;
Kevin Cozens's avatar
Kevin Cozens committed
121 122

  if (p == NULL)
123
    {
Kevin Cozens's avatar
Kevin Cozens committed
124
      p = g_new0 (Priv, 1);
125
      o->user_data = (void*) p;
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    }

  if (!inited)
    {
      av_register_all ();
      avcodec_register_all ();
      inited = 1;
    }

#ifndef DISABLE_AUDIO
  p->oxide_audio_instance = gggl_op_sym (op, "oxide_audio_instance");
  p->oxide_audio_query = gggl_op_sym (op, "oxide_audio_query()");
  p->oxide_audio_get_fragment =
    gggl_op_sym (op, "oxide_audio_get_fragment()");

  if (p->oxide_audio_instance && p->oxide_audio_query)
    {
      p->oxide_audio_query (p->oxide_audio_instance,
                            &p->sample_rate,
                            &p->bits,
                            &p->channels,
                            &p->fragment_samples, &p->fragment_size);

      /* FIXME: for now, the buffer is set to a size double that of a oxide
       * provided fragment,. should be enough no matter how things are handled,
       * but it should also be more than needed,. find out exact amount needed later
       */

      if (!p->buffer)
        {
Kevin Cozens's avatar
Kevin Cozens committed
156
          int size =
157 158 159 160 161 162 163 164 165 166
            (p->sample_rate / p->fps) * p->channels * (p->bits / 8) * 2;
          buffer_open (op, size);
        }

      if (!p->fragment)
        p->fragment = gggl_op_calloc (op, 1, p->fragment_size);
    }
#endif
}

Kevin Cozens's avatar
Kevin Cozens committed
167 168 169 170 171 172
static void close_video       (Priv            *p,
                               AVFormatContext *oc,
                               AVStream        *st);
void        close_audio       (Priv            *p,
                               AVFormatContext *oc,
                               AVStream        *st);
173 174
static int  tfile             (GeglProperties      *self);
static void write_video_frame (GeglProperties      *self,
Kevin Cozens's avatar
Kevin Cozens committed
175 176
                               AVFormatContext *oc,
                               AVStream        *st);
177
static void write_audio_frame (GeglProperties      *self,
Kevin Cozens's avatar
Kevin Cozens committed
178 179
                               AVFormatContext *oc,
                               AVStream        *st);
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213




#define STREAM_FRAME_RATE 25    /* 25 images/s */

static int
buffer_used (Priv * p)
{
  int       ret;
  if (!p || !p->buffer_size || !p->buffer)
    return -1;

  if (p->buffer_write_pos == p->buffer_read_pos)
    return 0;
  if (p->buffer_write_pos > p->buffer_read_pos)
    {
      ret = p->buffer_write_pos - p->buffer_read_pos;
    }
  else
    {
      ret = p->buffer_size - (p->buffer_read_pos - p->buffer_write_pos);
    }
  return ret;
}

static int
buffer_unused (Priv * p)
{
  int       ret;
  ret = p->buffer_size - buffer_used (p) - 1;   /* 1 byte for indicating full */
  return ret;
}

214
#ifndef DISABLE_AUDIO
215 216 217 218 219 220 221 222 223 224
static void
buffer_flush (Priv * p)
{
  if (!p->buffer)
    return;
  p->buffer_write_pos = 0;
  p->buffer_read_pos = 0;
}

static void
225
buffer_open (GeglOpOperation *op, int size)
226 227 228 229 230 231 232 233 234 235 236 237 238 239
{
  Priv     *p = (Priv*)op->priv;

  if (p->buffer)
    {
      fprintf (stderr, "double init of buffer, eek!\n");
    }

  p->buffer_size = size;
  p->buffer = g_malloc (size);
  buffer_flush (p);
}

static void
240
buffer_close (GeglOpOperation *op)
241 242 243 244 245 246 247 248 249
{
  Priv     *p = (Priv*)op->priv;

  if (!p->buffer)
    return;
  g_free (p->buffer);
  p->buffer = NULL;
}

250 251
#endif

252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
static int
buffer_write (Priv * p, uint8_t * source, int count)
{
  int       first_segment_size = 0;
  int       second_segment_size = 0;

  /* check if we have room for the data in the buffer */
  if (!p->buffer || buffer_unused (p) < count + 1)
    {
      fprintf (stderr, "ff_save audio buffer full!! shouldn't happen\n");
      return -1;
    }

  /* calculate size of segments to write */
  first_segment_size = p->buffer_size - p->buffer_write_pos;

  if (p->buffer_read_pos > p->buffer_write_pos)
    first_segment_size = p->buffer_read_pos - p->buffer_write_pos;

  if (first_segment_size >= count)
    {
      first_segment_size = count;
    }
  else
    {
      second_segment_size = count - first_segment_size;
    }

  memcpy (p->buffer + p->buffer_write_pos, source, first_segment_size);
  p->buffer_write_pos += first_segment_size;

  if (p->buffer_write_pos == p->buffer_size)
    p->buffer_write_pos = 0;

  if (second_segment_size)
    {
      memcpy (p->buffer + p->buffer_write_pos, source + first_segment_size,
              second_segment_size);
      p->buffer_write_pos = second_segment_size;
    }

  return first_segment_size + second_segment_size;
}

#if 0
. = unused byte
# = used byte
  all sizes are specified in contiguous bytes of memory
  __write_pos / __read_pos
  //
  |................................... | empty buffer
  \ _________________________________ /
  size_ / __read_pos __write_pos / /|........
#################..........|
  \ ______________ / used ()_ / __write_pos __read_pos / /|
############..................#####|
  \______________ / unused ()_ / ___write_pos / __read_pos / /|
#################.#################|    full buffer
#endif
     static int
     buffer_read (Priv * p, uint8_t * dest, int count)
{
  int       first_segment_size = 0;
  int       second_segment_size = 0;

  /* check if we have enough data to fulfil request */
  if (!p->buffer || buffer_used (p) < count)
    {
      fprintf (stderr, "ff_save audio buffer doesn't have enough data\n");
      return -1;
    }

  /* calculate size of segments to write */
  first_segment_size = p->buffer_size - p->buffer_read_pos;

  if (p->buffer_write_pos > p->buffer_read_pos)
    first_segment_size = p->buffer_write_pos - p->buffer_read_pos;

  if (first_segment_size >= count)
    {
      first_segment_size = count;
    }
  else
    {
      second_segment_size = count - first_segment_size;
    }

  memcpy (dest, p->buffer + p->buffer_read_pos, first_segment_size);
  p->buffer_read_pos += first_segment_size;

  if (p->buffer_read_pos == p->buffer_size)
    p->buffer_read_pos = 0;

  if (second_segment_size)
    {
      memcpy (dest + first_segment_size, p->buffer + p->buffer_read_pos,
              second_segment_size);
      p->buffer_read_pos = second_segment_size;
    }

  return first_segment_size + second_segment_size;
}

355
#ifndef DISABLE_AUDIO
356 357
/* add an audio output stream */
static AVStream *
358
add_audio_stream (GeglOpOperation *op, AVFormatContext * oc, int codec_id)
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
{
  Priv     *p = (Priv*)op->priv;
  AVCodecContext *c;
  AVStream *st;

  p = NULL;
  st = av_new_stream (oc, 1);
  if (!st)
    {
      fprintf (stderr, "Could not alloc stream\n");
      exit (1);
    }

  c = st->codec;
  c->codec_id = codec_id;
  c->codec_type = CODEC_TYPE_AUDIO;

  c->bit_rate = 64000;
  c->sample_rate = 44100;
  c->channels = 2;
  return st;
}
381
#endif
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399

static void
open_audio (Priv * p, AVFormatContext * oc, AVStream * st)
{
  AVCodecContext *c;
  AVCodec  *codec;

  c = st->codec;

  /* find the audio encoder */
  codec = avcodec_find_encoder (c->codec_id);
  if (!codec)
    {
      fprintf (stderr, "codec not found\n");
      exit (1);
    }

  /* open it */
400
  if (avcodec_open2 (c, codec, NULL) < 0)
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
    {
      fprintf (stderr, "could not open codec\n");
      exit (1);
    }

  p->audio_outbuf_size = 10000;
  p->audio_outbuf = malloc (p->audio_outbuf_size);

  /* ugly hack for PCM codecs (will be removed ASAP with new PCM
     support to compute the input frame size in samples */
  if (c->frame_size <= 1)
    {
      fprintf (stderr, "eeko\n");
      p->audio_input_frame_size = p->audio_outbuf_size / c->channels;
      switch (st->codec->codec_id)
        {
        case CODEC_ID_PCM_S16LE:
        case CODEC_ID_PCM_S16BE:
        case CODEC_ID_PCM_U16LE:
        case CODEC_ID_PCM_U16BE:
          p->audio_input_frame_size >>= 1;
          break;
        default:
          break;
        }
    }
  else
    {
      p->audio_input_frame_size = c->frame_size;
    }
431
  /*audio_input_frame_size = 44100/25;*/
432 433 434 435
  p->samples = malloc (p->audio_input_frame_size * 2 * c->channels);
}

void
436
write_audio_frame (GeglProperties *op, AVFormatContext * oc, AVStream * st)
437
{
438
  Priv *p = (Priv*)op->user_data;
439 440 441 442 443 444 445

  AVCodecContext *c;
  AVPacket  pkt;
  av_init_packet (&pkt);

  c = st->codec;

446
  /*fprintf (stderr, "going to grab %i\n", p->fragment_size);*/
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
  if (p->oxide_audio_get_fragment (p->oxide_audio_instance,
                                   p->fragment) == (signed) p->fragment_size)
    {
      buffer_write (p, p->fragment, p->fragment_size);
    }

  while (buffer_used (p) >= p->audio_input_frame_size * 2 * c->channels)
    {
      buffer_read (p, (uint8_t *) p->samples,
                   p->audio_input_frame_size * 2 * c->channels);

      pkt.size = avcodec_encode_audio (c, p->audio_outbuf,
                                       p->audio_outbuf_size, p->samples);

      pkt.pts = c->coded_frame->pts;
462
      pkt.flags |= AV_PKT_FLAG_KEY;
463 464 465 466 467 468 469 470 471 472 473
      pkt.stream_index = st->index;
      pkt.data = p->audio_outbuf;

      if (av_write_frame (oc, &pkt) != 0)
        {
          fprintf (stderr, "Error while writing audio frame\n");
          exit (1);
        }
    }
}

474
/*p->audio_get_frame (samples, audio_input_frame_size, c->channels);*/
475 476 477 478 479 480 481 482 483 484 485 486

void
close_audio (Priv * p, AVFormatContext * oc, AVStream * st)
{
  avcodec_close (st->codec);

  av_free (p->samples);
  av_free (p->audio_outbuf);
}

/* add a video output stream */
static AVStream *
487
add_video_stream (GeglProperties *op, AVFormatContext * oc, int codec_id)
488
{
489
  Priv *p = (Priv*)op->user_data;
490 491 492 493

  AVCodecContext *c;
  AVStream *st;

494
  st = avformat_new_stream (oc, NULL);
495 496 497 498 499 500 501 502
  if (!st)
    {
      fprintf (stderr, "Could not alloc stream %p %p %i\n", op, oc, codec_id);
      exit (1);
    }

  c = st->codec;
  c->codec_id = codec_id;
503
  c->codec_type = AVMEDIA_TYPE_VIDEO;
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530

  /* put sample propeters */
  c->bit_rate = op->bitrate;
  /* resolution must be a multiple of two */
  c->width = p->width;
  c->height = p->height;
  /* frames per second */
  /*c->frame_rate = op->fps;
  c->frame_rate_base = 1;*/

#if LIBAVCODEC_BUILD >= 4754
  c->time_base=(AVRational){1, op->fps};
    #else
        c->frame_rate=op->fps;
        c->frame_rate_base=1;
    #endif
     c->pix_fmt = PIX_FMT_YUV420P;


  c->gop_size = 12;             /* emit one intra frame every twelve frames at most */
  if (c->codec_id == CODEC_ID_MPEG2VIDEO)
    {
      /* just for testing, we also add B frames */
      /*c->max_b_frames = 2;*/
    }
/*    if (!strcmp (oc->oformat->name, "mp4") ||
          !strcmp (oc->oformat->name, "3gp"))
531
    c->flags |= CODEC_FLAG_GLOBAL_HEADER;
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
    */
  return st;
}


static AVFrame *
alloc_picture (int pix_fmt, int width, int height)
{
  AVFrame  *picture;
  uint8_t  *picture_buf;
  int       size;

  picture = avcodec_alloc_frame ();
  if (!picture)
    return NULL;
  size = avpicture_get_size (pix_fmt, width, height);
  picture_buf = malloc (size);
  if (!picture_buf)
    {
      av_free (picture);
      return NULL;
    }
  avpicture_fill ((AVPicture *) picture, picture_buf, pix_fmt, width, height);
  return picture;
}

static void
open_video (Priv * p, AVFormatContext * oc, AVStream * st)
{
  AVCodec  *codec;
  AVCodecContext *c;

  c = st->codec;

  /* find the video encoder */
  codec = avcodec_find_encoder (c->codec_id);
  if (!codec)
    {
      fprintf (stderr, "codec not found\n");
      exit (1);
    }

  /* open the codec */
575
  if (avcodec_open2 (c, codec, NULL) < 0)
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
    {
      fprintf (stderr, "could not open codec\n");
      exit (1);
    }

  p->video_outbuf = NULL;
  if (!(oc->oformat->flags & AVFMT_RAWPICTURE))
    {
      /* allocate output buffer */
      /* XXX: API change will be done */
      p->video_outbuf_size = 200000;
      p->video_outbuf = malloc (p->video_outbuf_size);
    }

  /* allocate the encoded raw picture */
  p->picture = alloc_picture (c->pix_fmt, c->width, c->height);
  if (!p->picture)
    {
      fprintf (stderr, "Could not allocate picture\n");
      exit (1);
    }

  /* if the output format is not YUV420P, then a temporary YUV420P
     picture is needed too. It is then converted to the required
     output format */
  p->tmp_picture = NULL;
  if (c->pix_fmt != PIX_FMT_RGB24)
    {
      p->tmp_picture = alloc_picture (PIX_FMT_RGB24, c->width, c->height);
      if (!p->tmp_picture)
        {
          fprintf (stderr, "Could not allocate temporary picture\n");
          exit (1);
        }
    }
}

static void
close_video (Priv * p, AVFormatContext * oc, AVStream * st)
{
  avcodec_close (st->codec);
  av_free (p->picture->data[0]);
  av_free (p->picture);
  if (p->tmp_picture)
    {
      av_free (p->tmp_picture->data[0]);
      av_free (p->tmp_picture);
    }
  av_free (p->video_outbuf);
}

#include "string.h"

/* prepare a dummy image */
static void
631
fill_yuv_image (GeglProperties *op,
Kevin Cozens's avatar
Kevin Cozens committed
632
                AVFrame *pict, int frame_index, int width, int height)
633
{
634
  Priv     *p = (Priv*)op->user_data;
635 636
  /*memcpy (pict->data[0],

637 638 639 640
   op->input_pad[0]->data,

          op->input_pad[0]->width * op->input_pad[0]->height * 3);*/
  GeglRectangle rect={0,0,width,height};
641
  gegl_buffer_get (p->input, &rect, 1.0, babl_format ("R'G'B' u8"), pict->data[0], GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
642 643 644
}

static void
645
write_video_frame (GeglProperties *op,
Kevin Cozens's avatar
Kevin Cozens committed
646
                   AVFormatContext *oc, AVStream *st)
647
{
648
  Priv     *p = (Priv*)op->user_data;
649 650 651 652 653 654 655 656
  int       out_size, ret;
  AVCodecContext *c;
  AVFrame  *picture_ptr;

  c = st->codec;

  if (c->pix_fmt != PIX_FMT_RGB24)
    {
657 658
      struct SwsContext *img_convert_ctx;

659 660 661 662
      /* as we only generate a RGB24 picture, we must convert it
         to the codec pixel format if needed */
      fill_yuv_image (op, p->tmp_picture, p->frame_count, c->width,
                      c->height);
663 664 665 666 667 668 669 670 671 672 673 674

      img_convert_ctx = sws_getContext(c->width, c->height, c->pix_fmt,
                                       c->width, c->height, PIX_FMT_RGB24,
                                       SWS_BICUBIC, NULL, NULL, NULL);

      if (img_convert_ctx == NULL)
        {
          fprintf(stderr, "ff_save: Cannot initialize conversion context.");
        }
      else
        {
          sws_scale(img_convert_ctx,
675
                    (void*) p->tmp_picture->data,
676 677 678 679 680 681
                    p->tmp_picture->linesize,
                    0,
                    c->height,
                    p->picture->data,
                    p->picture->linesize);
        }
682 683 684 685 686 687 688 689 690 691 692 693 694 695
    }
  else
    {
      fill_yuv_image (op, p->picture, p->frame_count, c->width, c->height);
    }
  picture_ptr = p->picture;

  if (oc->oformat->flags & AVFMT_RAWPICTURE)
    {
      /* raw video case. The API will change slightly in the near
         future for that */
      AVPacket  pkt;
      av_init_packet (&pkt);

696
      pkt.flags |= AV_PKT_FLAG_KEY;
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
      pkt.stream_index = st->index;
      pkt.data = (uint8_t *) picture_ptr;
      pkt.size = sizeof (AVPicture);

      ret = av_write_frame (oc, &pkt);
    }
  else
    {
      /* encode the image */
      out_size =
        avcodec_encode_video (c,
                              p->video_outbuf,
                              p->video_outbuf_size, picture_ptr);

      /* if zero size, it means the image was buffered */
      if (out_size != 0)
        {
          AVPacket  pkt;
          av_init_packet (&pkt);

          pkt.pts = c->coded_frame->pts;
          if (c->coded_frame->key_frame)
719
            pkt.flags |= AV_PKT_FLAG_KEY;
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
          pkt.stream_index = st->index;
          pkt.data = p->video_outbuf;
          pkt.size = out_size;

          /* write the compressed frame in the media file */
          ret = av_write_frame (oc, &pkt);
        }
      else
        {
          ret = 0;
        }
    }
  if (ret != 0)
    {
      fprintf (stderr, "Error while writing video frame\n");
      exit (1);
    }
  p->frame_count++;
}

static int
741
tfile (GeglProperties *self)
742
{
743
  Priv *p = (Priv*)self->user_data;
744

745
  p->fmt = av_guess_format (NULL, self->path, NULL);
746 747 748 749 750
  if (!p->fmt)
    {
      fprintf (stderr,
               "ff_save couldn't deduce outputformat from file extension: using MPEG.\n%s",
               "");
751
      p->fmt = av_guess_format ("mpeg", NULL, NULL);
752
    }
753
  p->oc = avformat_alloc_context ();/*g_malloc (sizeof (AVFormatContext));*/
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
  if (!p->oc)
    {
      fprintf (stderr, "memory error\n%s", "");
      return -1;
    }

  p->oc->oformat = p->fmt;

  snprintf (p->oc->filename, sizeof (p->oc->filename), "%s", self->path);

  p->video_st = NULL;
  p->audio_st = NULL;

  if (p->fmt->video_codec != CODEC_ID_NONE)
    {
      p->video_st = add_video_stream (self, p->oc, p->fmt->video_codec);
    }
  if (p->oxide_audio_query && p->fmt->audio_codec != CODEC_ID_NONE)
    {
773
     /*XXX: FOO p->audio_st = add_audio_stream (op, p->oc, p->fmt->audio_codec);*/
774 775
    }

776
#if 0  /* ick - grid */
777 778 779 780 781
  if (av_set_parameters (p->oc, NULL) < 0)
    {
      fprintf (stderr, "Invalid output format propeters\n%s", "");
      return -1;
    }
782
#endif
783

784
  av_dump_format (p->oc, 0, self->path, 1);
785 786 787 788 789 790

  if (p->video_st)
    open_video (p, p->oc, p->video_st);
  if (p->audio_st)
    open_audio (p, p->oc, p->audio_st);

791
  if (avio_open (&p->oc->pb, self->path, AVIO_FLAG_WRITE) < 0)
792 793 794 795 796
    {
      fprintf (stderr, "couldn't open '%s'\n", self->path);
      return -1;
    }

797 798
  //av_write_header (p->oc);
  avformat_write_header (p->oc, NULL);
799 800 801 802

  return 0;
}

803
#if 0
804
static int
805
filechanged (GeglOpOperation *op, const char *att)
806 807 808 809
{
  init (op);
  return 0;
}
810
#endif
811

Kevin Cozens's avatar
Kevin Cozens committed
812 813 814
static gboolean
process (GeglOperation       *operation,
         GeglBuffer          *input,
815 816
         const GeglRectangle *result,
         gint                 level)
Kevin Cozens's avatar
Kevin Cozens committed
817 818
{
  static gint inited = 0;
819 820
  GeglProperties *o = GEGL_PROPERTIES (operation);
  Priv       *p = (Priv*)o->user_data;
Kevin Cozens's avatar
Kevin Cozens committed
821 822 823 824 825

  g_assert (input);

  if (p == NULL)
    init (o);
826
  p = (Priv*)o->user_data;
Kevin Cozens's avatar
Kevin Cozens committed
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847

  p->width = result->width;
  p->height = result->height;
  p->input = input;

  if (!inited)
    {
      tfile (o);
      inited = 1;
    }

  write_video_frame (o, p->oc, p->video_st);
  if (p->audio_st)
    write_audio_frame (o, p->oc, p->audio_st);

  return  TRUE;
}

static void
finalize (GObject *object)
{
848 849
  GeglProperties *o = GEGL_PROPERTIES (object);
  if (o->user_data)
Kevin Cozens's avatar
Kevin Cozens committed
850
    {
851
      Priv *p = (Priv*)o->user_data;
Kevin Cozens's avatar
Kevin Cozens committed
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867

    if (p->oc)
      {
        gint i;
        if (p->video_st)
          close_video (p, p->oc, p->video_st);
        if (p->audio_st)
          close_audio (p, p->oc, p->audio_st);

        av_write_trailer (p->oc);

        for (i = 0; i < p->oc->nb_streams; i++)
          {
            av_freep (&p->oc->streams[i]);
          }

868
        avio_close (p->oc->pb);
Kevin Cozens's avatar
Kevin Cozens committed
869 870
        free (p->oc);
      }
871 872
      g_free (o->user_data);
      o->user_data = NULL;
Kevin Cozens's avatar
Kevin Cozens committed
873 874 875 876 877 878 879
    }

  G_OBJECT_CLASS (g_type_class_peek_parent (G_OBJECT_GET_CLASS (object)))->finalize (object);
}


static void
880
gegl_op_class_init (GeglOpClass *klass)
Kevin Cozens's avatar
Kevin Cozens committed
881 882 883 884 885 886 887 888 889 890 891 892
{
  GeglOperationClass     *operation_class;
  GeglOperationSinkClass *sink_class;

  G_OBJECT_CLASS (klass)->finalize = finalize;

  operation_class = GEGL_OPERATION_CLASS (klass);
  sink_class      = GEGL_OPERATION_SINK_CLASS (klass);

  sink_class->process = process;
  sink_class->needs_full = TRUE;

893 894 895 896 897
  gegl_operation_class_set_keys (operation_class,
    "name"        , "gegl:ff-save",
    "categories"  , "output:video",
    "description" , _("FFmpeg video output sink"),
    NULL);
Kevin Cozens's avatar
Kevin Cozens committed
898 899
}

900
#endif