Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright (c) 2015, Vignesh Venkatasubramanian
  3.  *
  4.  * This file is part of FFmpeg.
  5.  *
  6.  * FFmpeg is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * FFmpeg is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with FFmpeg; if not, write to the Free Software
  18.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19.  */
  20.  
  21. /**
  22.  * @file WebM Chunk Muxer
  23.  * The chunk muxer enables writing WebM Live chunks where there is a header
  24.  * chunk, followed by data chunks where each Cluster is written out as a Chunk.
  25.  */
  26.  
  27. #include <float.h>
  28. #include <time.h>
  29.  
  30. #include "avformat.h"
  31. #include "avio.h"
  32. #include "internal.h"
  33.  
  34. #include "libavutil/avassert.h"
  35. #include "libavutil/log.h"
  36. #include "libavutil/opt.h"
  37. #include "libavutil/avstring.h"
  38. #include "libavutil/parseutils.h"
  39. #include "libavutil/mathematics.h"
  40. #include "libavutil/time.h"
  41. #include "libavutil/time_internal.h"
  42. #include "libavutil/timestamp.h"
  43.  
  44. #define MAX_FILENAME_SIZE 1024
  45.  
  46. typedef struct WebMChunkContext {
  47.     const AVClass *class;
  48.     int chunk_start_index;
  49.     char *header_filename;
  50.     int chunk_duration;
  51.     int chunk_index;
  52.     uint64_t duration_written;
  53.     int prev_pts;
  54.     AVOutputFormat *oformat;
  55.     AVFormatContext *avf;
  56. } WebMChunkContext;
  57.  
  58. static int chunk_mux_init(AVFormatContext *s)
  59. {
  60.     WebMChunkContext *wc = s->priv_data;
  61.     AVFormatContext *oc;
  62.     int ret;
  63.  
  64.     ret = avformat_alloc_output_context2(&wc->avf, wc->oformat, NULL, NULL);
  65.     if (ret < 0)
  66.         return ret;
  67.     oc = wc->avf;
  68.  
  69.     oc->interrupt_callback = s->interrupt_callback;
  70.     oc->max_delay          = s->max_delay;
  71.     av_dict_copy(&oc->metadata, s->metadata, 0);
  72.  
  73.     *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
  74.     av_opt_set_defaults(oc->priv_data);
  75.     av_opt_set_int(oc->priv_data, "dash", 1, 0);
  76.     av_opt_set_int(oc->priv_data, "cluster_time_limit", wc->chunk_duration, 0);
  77.     av_opt_set_int(oc->priv_data, "live", 1, 0);
  78.  
  79.     oc->streams = s->streams;
  80.     oc->nb_streams = s->nb_streams;
  81.  
  82.     return 0;
  83. }
  84.  
  85. static int get_chunk_filename(AVFormatContext *s, int is_header, char *filename)
  86. {
  87.     WebMChunkContext *wc = s->priv_data;
  88.     AVFormatContext *oc = wc->avf;
  89.     if (!filename) {
  90.         return AVERROR(EINVAL);
  91.     }
  92.     if (is_header) {
  93.         if (!wc->header_filename) {
  94.             return AVERROR(EINVAL);
  95.         }
  96.         av_strlcpy(filename, wc->header_filename, strlen(wc->header_filename) + 1);
  97.     } else {
  98.         if (av_get_frame_filename(filename, MAX_FILENAME_SIZE,
  99.                                   s->filename, wc->chunk_index - 1) < 0) {
  100.             av_log(oc, AV_LOG_ERROR, "Invalid chunk filename template '%s'\n", s->filename);
  101.             return AVERROR(EINVAL);
  102.         }
  103.     }
  104.     return 0;
  105. }
  106.  
  107. static int webm_chunk_write_header(AVFormatContext *s)
  108. {
  109.     WebMChunkContext *wc = s->priv_data;
  110.     AVFormatContext *oc = NULL;
  111.     int ret;
  112.  
  113.     // DASH Streams can only have either one track per file.
  114.     if (s->nb_streams != 1) { return AVERROR_INVALIDDATA; }
  115.  
  116.     wc->chunk_index = wc->chunk_start_index;
  117.     wc->oformat = av_guess_format("webm", s->filename, "video/webm");
  118.     if (!wc->oformat)
  119.         return AVERROR_MUXER_NOT_FOUND;
  120.  
  121.     ret = chunk_mux_init(s);
  122.     if (ret < 0)
  123.         return ret;
  124.     oc = wc->avf;
  125.     ret = get_chunk_filename(s, 1, oc->filename);
  126.     if (ret < 0)
  127.         return ret;
  128.     ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  129.                      &s->interrupt_callback, NULL);
  130.     if (ret < 0)
  131.         return ret;
  132.  
  133.     oc->pb->seekable = 0;
  134.     ret = oc->oformat->write_header(oc);
  135.     if (ret < 0)
  136.         return ret;
  137.     avio_close(oc->pb);
  138.     return 0;
  139. }
  140.  
  141. static int chunk_start(AVFormatContext *s)
  142. {
  143.     WebMChunkContext *wc = s->priv_data;
  144.     AVFormatContext *oc = wc->avf;
  145.     int ret;
  146.  
  147.     ret = avio_open_dyn_buf(&oc->pb);
  148.     if (ret < 0)
  149.         return ret;
  150.     wc->chunk_index++;
  151.     return 0;
  152. }
  153.  
  154. static int chunk_end(AVFormatContext *s)
  155. {
  156.     WebMChunkContext *wc = s->priv_data;
  157.     AVFormatContext *oc = wc->avf;
  158.     int ret;
  159.     int buffer_size;
  160.     uint8_t *buffer;
  161.     AVIOContext *pb;
  162.     char filename[MAX_FILENAME_SIZE];
  163.  
  164.     if (wc->chunk_start_index == wc->chunk_index)
  165.         return 0;
  166.     // Flush the cluster in WebM muxer.
  167.     oc->oformat->write_packet(oc, NULL);
  168.     buffer_size = avio_close_dyn_buf(oc->pb, &buffer);
  169.     ret = get_chunk_filename(s, 0, filename);
  170.     if (ret < 0)
  171.         goto fail;
  172.     ret = avio_open2(&pb, filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
  173.     if (ret < 0)
  174.         goto fail;
  175.     avio_write(pb, buffer, buffer_size);
  176.     ret = avio_close(pb);
  177.     if (ret < 0)
  178.         goto fail;
  179.     oc->pb = NULL;
  180. fail:
  181.     av_free(buffer);
  182.     return (ret < 0) ? ret : 0;
  183. }
  184.  
  185. static int webm_chunk_write_packet(AVFormatContext *s, AVPacket *pkt)
  186. {
  187.     WebMChunkContext *wc = s->priv_data;
  188.     AVFormatContext *oc = wc->avf;
  189.     AVStream *st = s->streams[pkt->stream_index];
  190.     int ret;
  191.  
  192.     if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  193.         wc->duration_written += av_rescale_q(pkt->pts - wc->prev_pts,
  194.                                              st->time_base,
  195.                                              (AVRational) {1, 1000});
  196.         wc->prev_pts = pkt->pts;
  197.     }
  198.  
  199.     // For video, a new chunk is started only on key frames. For audio, a new
  200.     // chunk is started based on chunk_duration.
  201.     if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  202.          (pkt->flags & AV_PKT_FLAG_KEY)) ||
  203.         (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  204.          (pkt->pts == 0 || wc->duration_written >= wc->chunk_duration))) {
  205.         wc->duration_written = 0;
  206.         if ((ret = chunk_end(s)) < 0 || (ret = chunk_start(s)) < 0) {
  207.             goto fail;
  208.         }
  209.     }
  210.  
  211.     ret = oc->oformat->write_packet(oc, pkt);
  212.     if (ret < 0)
  213.         goto fail;
  214.  
  215. fail:
  216.     if (ret < 0) {
  217.         oc->streams = NULL;
  218.         oc->nb_streams = 0;
  219.         avformat_free_context(oc);
  220.     }
  221.  
  222.     return ret;
  223. }
  224.  
  225. static int webm_chunk_write_trailer(AVFormatContext *s)
  226. {
  227.     WebMChunkContext *wc = s->priv_data;
  228.     AVFormatContext *oc = wc->avf;
  229.     oc->oformat->write_trailer(oc);
  230.     chunk_end(s);
  231.     oc->streams = NULL;
  232.     oc->nb_streams = 0;
  233.     avformat_free_context(oc);
  234.     return 0;
  235. }
  236.  
  237. #define OFFSET(x) offsetof(WebMChunkContext, x)
  238. static const AVOption options[] = {
  239.     { "chunk_start_index",  "start index of the chunk", OFFSET(chunk_start_index), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  240.     { "header", "filename of the header where the initialization data will be written", OFFSET(header_filename), AV_OPT_TYPE_STRING, { 0 }, 0, 0, AV_OPT_FLAG_ENCODING_PARAM },
  241.     { "audio_chunk_duration", "duration of each chunk in milliseconds", OFFSET(chunk_duration), AV_OPT_TYPE_INT, {.i64 = 5000}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
  242.     { NULL },
  243. };
  244.  
  245. #if CONFIG_WEBM_CHUNK_MUXER
  246. static const AVClass webm_chunk_class = {
  247.     .class_name = "WebM Chunk Muxer",
  248.     .item_name  = av_default_item_name,
  249.     .option     = options,
  250.     .version    = LIBAVUTIL_VERSION_INT,
  251. };
  252.  
  253. AVOutputFormat ff_webm_chunk_muxer = {
  254.     .name           = "webm_chunk",
  255.     .long_name      = NULL_IF_CONFIG_SMALL("WebM Chunk Muxer"),
  256.     .mime_type      = "video/webm",
  257.     .extensions     = "chk",
  258.     .flags          = AVFMT_NOFILE | AVFMT_GLOBALHEADER | AVFMT_NEEDNUMBER |
  259.                       AVFMT_TS_NONSTRICT,
  260.     .priv_data_size = sizeof(WebMChunkContext),
  261.     .write_header   = webm_chunk_write_header,
  262.     .write_packet   = webm_chunk_write_packet,
  263.     .write_trailer  = webm_chunk_write_trailer,
  264.     .priv_class     = &webm_chunk_class,
  265. };
  266. #endif
  267.