src/http/modules/ngx_http_mp4_module.c - nginx-1.31.3 nginx/ @ 42f8df65b

Global variables defined

Data types defined

Functions defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */

  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_http.h>


  8. #define NGX_HTTP_MP4_TRAK_ATOM     0
  9. #define NGX_HTTP_MP4_TKHD_ATOM     1
  10. #define NGX_HTTP_MP4_EDTS_ATOM     2
  11. #define NGX_HTTP_MP4_ELST_ATOM     3
  12. #define NGX_HTTP_MP4_MDIA_ATOM     4
  13. #define NGX_HTTP_MP4_MDHD_ATOM     5
  14. #define NGX_HTTP_MP4_HDLR_ATOM     6
  15. #define NGX_HTTP_MP4_MINF_ATOM     7
  16. #define NGX_HTTP_MP4_VMHD_ATOM     8
  17. #define NGX_HTTP_MP4_SMHD_ATOM     9
  18. #define NGX_HTTP_MP4_DINF_ATOM    10
  19. #define NGX_HTTP_MP4_STBL_ATOM    11
  20. #define NGX_HTTP_MP4_STSD_ATOM    12
  21. #define NGX_HTTP_MP4_STTS_ATOM    13
  22. #define NGX_HTTP_MP4_STTS_DATA    14
  23. #define NGX_HTTP_MP4_STSS_ATOM    15
  24. #define NGX_HTTP_MP4_STSS_DATA    16
  25. #define NGX_HTTP_MP4_CTTS_ATOM    17
  26. #define NGX_HTTP_MP4_CTTS_DATA    18
  27. #define NGX_HTTP_MP4_STSC_ATOM    19
  28. #define NGX_HTTP_MP4_STSC_START   20
  29. #define NGX_HTTP_MP4_STSC_DATA    21
  30. #define NGX_HTTP_MP4_STSC_END     22
  31. #define NGX_HTTP_MP4_STSZ_ATOM    23
  32. #define NGX_HTTP_MP4_STSZ_DATA    24
  33. #define NGX_HTTP_MP4_STCO_ATOM    25
  34. #define NGX_HTTP_MP4_STCO_DATA    26
  35. #define NGX_HTTP_MP4_CO64_ATOM    27
  36. #define NGX_HTTP_MP4_CO64_DATA    28

  37. #define NGX_HTTP_MP4_LAST_ATOM    NGX_HTTP_MP4_CO64_DATA


  38. typedef struct {
  39.     size_t                buffer_size;
  40.     size_t                max_buffer_size;
  41.     ngx_flag_t            start_key_frame;
  42. } ngx_http_mp4_conf_t;


  43. typedef struct {
  44.     u_char                chunk[4];
  45.     u_char                samples[4];
  46.     u_char                id[4];
  47. } ngx_mp4_stsc_entry_t;


  48. typedef struct {
  49.     u_char                size[4];
  50.     u_char                name[4];
  51. } ngx_mp4_edts_atom_t;


  52. typedef struct {
  53.     u_char                size[4];
  54.     u_char                name[4];
  55.     u_char                version[1];
  56.     u_char                flags[3];
  57.     u_char                entries[4];
  58.     u_char                duration[8];
  59.     u_char                media_time[8];
  60.     u_char                media_rate[2];
  61.     u_char                reserved[2];
  62. } ngx_mp4_elst_atom_t;


  63. typedef struct {
  64.     uint32_t              timescale;
  65.     uint32_t              time_to_sample_entries;
  66.     uint32_t              sample_to_chunk_entries;
  67.     uint32_t              sync_samples_entries;
  68.     uint32_t              composition_offset_entries;
  69.     uint32_t              sample_sizes_entries;
  70.     uint32_t              chunks;

  71.     ngx_uint_t            start_sample;
  72.     ngx_uint_t            end_sample;
  73.     ngx_uint_t            start_chunk;
  74.     ngx_uint_t            end_chunk;
  75.     ngx_uint_t            start_chunk_samples;
  76.     ngx_uint_t            end_chunk_samples;
  77.     uint64_t              start_chunk_samples_size;
  78.     uint64_t              end_chunk_samples_size;
  79.     uint64_t              duration;
  80.     uint64_t              prefix;
  81.     uint64_t              movie_duration;
  82.     off_t                 start_offset;
  83.     off_t                 end_offset;

  84.     size_t                tkhd_size;
  85.     size_t                mdhd_size;
  86.     size_t                hdlr_size;
  87.     size_t                vmhd_size;
  88.     size_t                smhd_size;
  89.     size_t                dinf_size;
  90.     size_t                size;

  91.     ngx_chain_t           out[NGX_HTTP_MP4_LAST_ATOM + 1];

  92.     ngx_buf_t             trak_atom_buf;
  93.     ngx_buf_t             tkhd_atom_buf;
  94.     ngx_buf_t             edts_atom_buf;
  95.     ngx_buf_t             elst_atom_buf;
  96.     ngx_buf_t             mdia_atom_buf;
  97.     ngx_buf_t             mdhd_atom_buf;
  98.     ngx_buf_t             hdlr_atom_buf;
  99.     ngx_buf_t             minf_atom_buf;
  100.     ngx_buf_t             vmhd_atom_buf;
  101.     ngx_buf_t             smhd_atom_buf;
  102.     ngx_buf_t             dinf_atom_buf;
  103.     ngx_buf_t             stbl_atom_buf;
  104.     ngx_buf_t             stsd_atom_buf;
  105.     ngx_buf_t             stts_atom_buf;
  106.     ngx_buf_t             stts_data_buf;
  107.     ngx_buf_t             stss_atom_buf;
  108.     ngx_buf_t             stss_data_buf;
  109.     ngx_buf_t             ctts_atom_buf;
  110.     ngx_buf_t             ctts_data_buf;
  111.     ngx_buf_t             stsc_atom_buf;
  112.     ngx_buf_t             stsc_start_chunk_buf;
  113.     ngx_buf_t             stsc_end_chunk_buf;
  114.     ngx_buf_t             stsc_data_buf;
  115.     ngx_buf_t             stsz_atom_buf;
  116.     ngx_buf_t             stsz_data_buf;
  117.     ngx_buf_t             stco_atom_buf;
  118.     ngx_buf_t             stco_data_buf;
  119.     ngx_buf_t             co64_atom_buf;
  120.     ngx_buf_t             co64_data_buf;

  121.     ngx_mp4_edts_atom_t   edts_atom;
  122.     ngx_mp4_elst_atom_t   elst_atom;
  123.     ngx_mp4_stsc_entry_t  stsc_start_chunk_entry;
  124.     ngx_mp4_stsc_entry_t  stsc_end_chunk_entry;
  125. } ngx_http_mp4_trak_t;


  126. typedef struct {
  127.     ngx_file_t            file;

  128.     u_char               *buffer;
  129.     u_char               *buffer_start;
  130.     u_char               *buffer_pos;
  131.     u_char               *buffer_end;
  132.     size_t                buffer_size;

  133.     off_t                 offset;
  134.     off_t                 end;
  135.     off_t                 content_length;
  136.     ngx_uint_t            start;
  137.     ngx_uint_t            length;
  138.     uint32_t              timescale;
  139.     ngx_http_request_t   *request;
  140.     ngx_array_t           trak;
  141.     ngx_http_mp4_trak_t   traks[2];

  142.     size_t                ftyp_size;
  143.     size_t                moov_size;

  144.     ngx_chain_t          *out;
  145.     ngx_chain_t           ftyp_atom;
  146.     ngx_chain_t           moov_atom;
  147.     ngx_chain_t           mvhd_atom;
  148.     ngx_chain_t           mdat_atom;
  149.     ngx_chain_t           mdat_data;

  150.     ngx_buf_t             ftyp_atom_buf;
  151.     ngx_buf_t             moov_atom_buf;
  152.     ngx_buf_t             mvhd_atom_buf;
  153.     ngx_buf_t             mdat_atom_buf;
  154.     ngx_buf_t             mdat_data_buf;

  155.     u_char                moov_atom_header[8];
  156.     u_char                mdat_atom_header[16];
  157. } ngx_http_mp4_file_t;


  158. typedef struct {
  159.     char                 *name;
  160.     ngx_int_t           (*handler)(ngx_http_mp4_file_t *mp4,
  161.                                    uint64_t atom_data_size);
  162. } ngx_http_mp4_atom_handler_t;


  163. #define ngx_mp4_atom_header(mp4)   (mp4->buffer_pos - 8)
  164. #define ngx_mp4_atom_data(mp4)     mp4->buffer_pos
  165. #define ngx_mp4_atom_data_size(t)  (uint64_t) (sizeof(t) - 8)


  166. #define ngx_mp4_atom_next(mp4, n)                                             \
  167.                                                                               \
  168.     if (n > (size_t) (mp4->buffer_end - mp4->buffer_pos)) {                   \
  169.         mp4->buffer_pos = mp4->buffer_end;                                    \
  170.                                                                               \
  171.     } else {                                                                  \
  172.         mp4->buffer_pos += (size_t) n;                                        \
  173.     }                                                                         \
  174.                                                                               \
  175.     mp4->offset += n


  176. #define ngx_mp4_set_atom_name(p, n1, n2, n3, n4)                              \
  177.     ((u_char *) (p))[4] = n1;                                                 \
  178.     ((u_char *) (p))[5] = n2;                                                 \
  179.     ((u_char *) (p))[6] = n3;                                                 \
  180.     ((u_char *) (p))[7] = n4

  181. #define ngx_mp4_get_16value(p)                                                \
  182.     ( ((uint16_t) ((u_char *) (p))[0] << 8)                                   \
  183.     + (           ((u_char *) (p))[1]) )

  184. #define ngx_mp4_set_16value(p, n)                                             \
  185.     ((u_char *) (p))[0] = (u_char) ((n) >> 8);                                \
  186.     ((u_char *) (p))[1] = (u_char)  (n)

  187. #define ngx_mp4_get_32value(p)                                                \
  188.     ( ((uint32_t) ((u_char *) (p))[0] << 24)                                  \
  189.     + (           ((u_char *) (p))[1] << 16)                                  \
  190.     + (           ((u_char *) (p))[2] << 8)                                   \
  191.     + (           ((u_char *) (p))[3]) )

  192. #define ngx_mp4_set_32value(p, n)                                             \
  193.     ((u_char *) (p))[0] = (u_char) ((n) >> 24);                               \
  194.     ((u_char *) (p))[1] = (u_char) ((n) >> 16);                               \
  195.     ((u_char *) (p))[2] = (u_char) ((n) >> 8);                                \
  196.     ((u_char *) (p))[3] = (u_char)  (n)

  197. #define ngx_mp4_get_64value(p)                                                \
  198.     ( ((uint64_t) ((u_char *) (p))[0] << 56)                                  \
  199.     + ((uint64_t) ((u_char *) (p))[1] << 48)                                  \
  200.     + ((uint64_t) ((u_char *) (p))[2] << 40)                                  \
  201.     + ((uint64_t) ((u_char *) (p))[3] << 32)                                  \
  202.     + ((uint64_t) ((u_char *) (p))[4] << 24)                                  \
  203.     + (           ((u_char *) (p))[5] << 16)                                  \
  204.     + (           ((u_char *) (p))[6] << 8)                                   \
  205.     + (           ((u_char *) (p))[7]) )

  206. #define ngx_mp4_set_64value(p, n)                                             \
  207.     ((u_char *) (p))[0] = (u_char) ((uint64_t) (n) >> 56);                    \
  208.     ((u_char *) (p))[1] = (u_char) ((uint64_t) (n) >> 48);                    \
  209.     ((u_char *) (p))[2] = (u_char) ((uint64_t) (n) >> 40);                    \
  210.     ((u_char *) (p))[3] = (u_char) ((uint64_t) (n) >> 32);                    \
  211.     ((u_char *) (p))[4] = (u_char) (           (n) >> 24);                    \
  212.     ((u_char *) (p))[5] = (u_char) (           (n) >> 16);                    \
  213.     ((u_char *) (p))[6] = (u_char) (           (n) >> 8);                     \
  214.     ((u_char *) (p))[7] = (u_char)             (n)

  215. #define ngx_mp4_last_trak(mp4)                                                \
  216.     &((ngx_http_mp4_trak_t *) mp4->trak.elts)[mp4->trak.nelts - 1]


  217. static ngx_int_t ngx_http_mp4_handler(ngx_http_request_t *r);
  218. static ngx_int_t ngx_http_mp4_atofp(u_char *line, size_t n, size_t point);

  219. static ngx_int_t ngx_http_mp4_process(ngx_http_mp4_file_t *mp4);
  220. static ngx_int_t ngx_http_mp4_read_atom(ngx_http_mp4_file_t *mp4,
  221.     ngx_http_mp4_atom_handler_t *atom, uint64_t atom_data_size);
  222. static ngx_int_t ngx_http_mp4_read(ngx_http_mp4_file_t *mp4, size_t size);
  223. static ngx_int_t ngx_http_mp4_read_ftyp_atom(ngx_http_mp4_file_t *mp4,
  224.     uint64_t atom_data_size);
  225. static ngx_int_t ngx_http_mp4_read_moov_atom(ngx_http_mp4_file_t *mp4,
  226.     uint64_t atom_data_size);
  227. static ngx_int_t ngx_http_mp4_read_mdat_atom(ngx_http_mp4_file_t *mp4,
  228.     uint64_t atom_data_size);
  229. static size_t ngx_http_mp4_update_mdat_atom(ngx_http_mp4_file_t *mp4,
  230.     off_t start_offset, off_t end_offset);
  231. static ngx_int_t ngx_http_mp4_read_mvhd_atom(ngx_http_mp4_file_t *mp4,
  232.     uint64_t atom_data_size);
  233. static ngx_int_t ngx_http_mp4_read_trak_atom(ngx_http_mp4_file_t *mp4,
  234.     uint64_t atom_data_size);
  235. static void ngx_http_mp4_update_trak_atom(ngx_http_mp4_file_t *mp4,
  236.     ngx_http_mp4_trak_t *trak);
  237. static ngx_int_t ngx_http_mp4_read_cmov_atom(ngx_http_mp4_file_t *mp4,
  238.     uint64_t atom_data_size);
  239. static ngx_int_t ngx_http_mp4_read_tkhd_atom(ngx_http_mp4_file_t *mp4,
  240.     uint64_t atom_data_size);
  241. static ngx_int_t ngx_http_mp4_read_mdia_atom(ngx_http_mp4_file_t *mp4,
  242.     uint64_t atom_data_size);
  243. static void ngx_http_mp4_update_mdia_atom(ngx_http_mp4_file_t *mp4,
  244.     ngx_http_mp4_trak_t *trak);
  245. static ngx_int_t ngx_http_mp4_read_mdhd_atom(ngx_http_mp4_file_t *mp4,
  246.     uint64_t atom_data_size);
  247. static void ngx_http_mp4_update_mdhd_atom(ngx_http_mp4_file_t *mp4,
  248.     ngx_http_mp4_trak_t *trak);
  249. static ngx_int_t ngx_http_mp4_read_hdlr_atom(ngx_http_mp4_file_t *mp4,
  250.     uint64_t atom_data_size);
  251. static ngx_int_t ngx_http_mp4_read_minf_atom(ngx_http_mp4_file_t *mp4,
  252.     uint64_t atom_data_size);
  253. static void ngx_http_mp4_update_minf_atom(ngx_http_mp4_file_t *mp4,
  254.     ngx_http_mp4_trak_t *trak);
  255. static ngx_int_t ngx_http_mp4_read_dinf_atom(ngx_http_mp4_file_t *mp4,
  256.     uint64_t atom_data_size);
  257. static ngx_int_t ngx_http_mp4_read_vmhd_atom(ngx_http_mp4_file_t *mp4,
  258.     uint64_t atom_data_size);
  259. static ngx_int_t ngx_http_mp4_read_smhd_atom(ngx_http_mp4_file_t *mp4,
  260.     uint64_t atom_data_size);
  261. static ngx_int_t ngx_http_mp4_read_stbl_atom(ngx_http_mp4_file_t *mp4,
  262.     uint64_t atom_data_size);
  263. static void ngx_http_mp4_update_edts_atom(ngx_http_mp4_file_t *mp4,
  264.     ngx_http_mp4_trak_t *trak);
  265. static void ngx_http_mp4_update_stbl_atom(ngx_http_mp4_file_t *mp4,
  266.     ngx_http_mp4_trak_t *trak);
  267. static ngx_int_t ngx_http_mp4_read_stsd_atom(ngx_http_mp4_file_t *mp4,
  268.     uint64_t atom_data_size);
  269. static ngx_int_t ngx_http_mp4_read_stts_atom(ngx_http_mp4_file_t *mp4,
  270.     uint64_t atom_data_size);
  271. static ngx_int_t ngx_http_mp4_update_stts_atom(ngx_http_mp4_file_t *mp4,
  272.     ngx_http_mp4_trak_t *trak);
  273. static ngx_int_t ngx_http_mp4_crop_stts_data(ngx_http_mp4_file_t *mp4,
  274.     ngx_http_mp4_trak_t *trak, ngx_uint_t start);
  275. static ngx_int_t ngx_http_mp4_seek_key_frame(ngx_http_mp4_file_t *mp4,
  276.     ngx_http_mp4_trak_t *trak, uint32_t start_sample, uint32_t *key_prefix);
  277. static ngx_int_t ngx_http_mp4_read_stss_atom(ngx_http_mp4_file_t *mp4,
  278.     uint64_t atom_data_size);
  279. static ngx_int_t ngx_http_mp4_update_stss_atom(ngx_http_mp4_file_t *mp4,
  280.     ngx_http_mp4_trak_t *trak);
  281. static void ngx_http_mp4_crop_stss_data(ngx_http_mp4_file_t *mp4,
  282.     ngx_http_mp4_trak_t *trak, ngx_uint_t start);
  283. static ngx_int_t ngx_http_mp4_read_ctts_atom(ngx_http_mp4_file_t *mp4,
  284.     uint64_t atom_data_size);
  285. static void ngx_http_mp4_update_ctts_atom(ngx_http_mp4_file_t *mp4,
  286.     ngx_http_mp4_trak_t *trak);
  287. static void ngx_http_mp4_crop_ctts_data(ngx_http_mp4_file_t *mp4,
  288.     ngx_http_mp4_trak_t *trak, ngx_uint_t start);
  289. static ngx_int_t ngx_http_mp4_read_stsc_atom(ngx_http_mp4_file_t *mp4,
  290.     uint64_t atom_data_size);
  291. static ngx_int_t ngx_http_mp4_update_stsc_atom(ngx_http_mp4_file_t *mp4,
  292.     ngx_http_mp4_trak_t *trak);
  293. static ngx_int_t ngx_http_mp4_crop_stsc_data(ngx_http_mp4_file_t *mp4,
  294.     ngx_http_mp4_trak_t *trak, ngx_uint_t start);
  295. static ngx_int_t ngx_http_mp4_read_stsz_atom(ngx_http_mp4_file_t *mp4,
  296.     uint64_t atom_data_size);
  297. static ngx_int_t ngx_http_mp4_update_stsz_atom(ngx_http_mp4_file_t *mp4,
  298.     ngx_http_mp4_trak_t *trak);
  299. static ngx_int_t ngx_http_mp4_read_stco_atom(ngx_http_mp4_file_t *mp4,
  300.     uint64_t atom_data_size);
  301. static ngx_int_t ngx_http_mp4_update_stco_atom(ngx_http_mp4_file_t *mp4,
  302.     ngx_http_mp4_trak_t *trak);
  303. static void ngx_http_mp4_adjust_stco_atom(ngx_http_mp4_file_t *mp4,
  304.     ngx_http_mp4_trak_t *trak, int32_t adjustment);
  305. static ngx_int_t ngx_http_mp4_read_co64_atom(ngx_http_mp4_file_t *mp4,
  306.     uint64_t atom_data_size);
  307. static ngx_int_t ngx_http_mp4_update_co64_atom(ngx_http_mp4_file_t *mp4,
  308.     ngx_http_mp4_trak_t *trak);
  309. static void ngx_http_mp4_adjust_co64_atom(ngx_http_mp4_file_t *mp4,
  310.     ngx_http_mp4_trak_t *trak, off_t adjustment);

  311. static char *ngx_http_mp4(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
  312. static void *ngx_http_mp4_create_conf(ngx_conf_t *cf);
  313. static char *ngx_http_mp4_merge_conf(ngx_conf_t *cf, void *parent, void *child);


  314. static ngx_command_t  ngx_http_mp4_commands[] = {

  315.     { ngx_string("mp4"),
  316.       NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
  317.       ngx_http_mp4,
  318.       0,
  319.       0,
  320.       NULL },

  321.     { ngx_string("mp4_buffer_size"),
  322.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  323.       ngx_conf_set_size_slot,
  324.       NGX_HTTP_LOC_CONF_OFFSET,
  325.       offsetof(ngx_http_mp4_conf_t, buffer_size),
  326.       NULL },

  327.     { ngx_string("mp4_max_buffer_size"),
  328.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  329.       ngx_conf_set_size_slot,
  330.       NGX_HTTP_LOC_CONF_OFFSET,
  331.       offsetof(ngx_http_mp4_conf_t, max_buffer_size),
  332.       NULL },

  333.     { ngx_string("mp4_start_key_frame"),
  334.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
  335.       ngx_conf_set_flag_slot,
  336.       NGX_HTTP_LOC_CONF_OFFSET,
  337.       offsetof(ngx_http_mp4_conf_t, start_key_frame),
  338.       NULL },

  339.       ngx_null_command
  340. };


  341. static ngx_http_module_t  ngx_http_mp4_module_ctx = {
  342.     NULL,                          /* preconfiguration */
  343.     NULL,                          /* postconfiguration */

  344.     NULL,                          /* create main configuration */
  345.     NULL,                          /* init main configuration */

  346.     NULL,                          /* create server configuration */
  347.     NULL,                          /* merge server configuration */

  348.     ngx_http_mp4_create_conf,      /* create location configuration */
  349.     ngx_http_mp4_merge_conf        /* merge location configuration */
  350. };


  351. ngx_module_t  ngx_http_mp4_module = {
  352.     NGX_MODULE_V1,
  353.     &ngx_http_mp4_module_ctx,      /* module context */
  354.     ngx_http_mp4_commands,         /* module directives */
  355.     NGX_HTTP_MODULE,               /* module type */
  356.     NULL,                          /* init master */
  357.     NULL,                          /* init module */
  358.     NULL,                          /* init process */
  359.     NULL,                          /* init thread */
  360.     NULL,                          /* exit thread */
  361.     NULL,                          /* exit process */
  362.     NULL,                          /* exit master */
  363.     NGX_MODULE_V1_PADDING
  364. };


  365. static ngx_http_mp4_atom_handler_t  ngx_http_mp4_atoms[] = {
  366.     { "ftyp", ngx_http_mp4_read_ftyp_atom },
  367.     { "moov", ngx_http_mp4_read_moov_atom },
  368.     { "mdat", ngx_http_mp4_read_mdat_atom },
  369.     { NULL, NULL }
  370. };

  371. static ngx_http_mp4_atom_handler_t  ngx_http_mp4_moov_atoms[] = {
  372.     { "mvhd", ngx_http_mp4_read_mvhd_atom },
  373.     { "trak", ngx_http_mp4_read_trak_atom },
  374.     { "cmov", ngx_http_mp4_read_cmov_atom },
  375.     { NULL, NULL }
  376. };

  377. static ngx_http_mp4_atom_handler_t  ngx_http_mp4_trak_atoms[] = {
  378.     { "tkhd", ngx_http_mp4_read_tkhd_atom },
  379.     { "mdia", ngx_http_mp4_read_mdia_atom },
  380.     { NULL, NULL }
  381. };

  382. static ngx_http_mp4_atom_handler_t  ngx_http_mp4_mdia_atoms[] = {
  383.     { "mdhd", ngx_http_mp4_read_mdhd_atom },
  384.     { "hdlr", ngx_http_mp4_read_hdlr_atom },
  385.     { "minf", ngx_http_mp4_read_minf_atom },
  386.     { NULL, NULL }
  387. };

  388. static ngx_http_mp4_atom_handler_t  ngx_http_mp4_minf_atoms[] = {
  389.     { "vmhd", ngx_http_mp4_read_vmhd_atom },
  390.     { "smhd", ngx_http_mp4_read_smhd_atom },
  391.     { "dinf", ngx_http_mp4_read_dinf_atom },
  392.     { "stbl", ngx_http_mp4_read_stbl_atom },
  393.     { NULL, NULL }
  394. };

  395. static ngx_http_mp4_atom_handler_t  ngx_http_mp4_stbl_atoms[] = {
  396.     { "stsd", ngx_http_mp4_read_stsd_atom },
  397.     { "stts", ngx_http_mp4_read_stts_atom },
  398.     { "stss", ngx_http_mp4_read_stss_atom },
  399.     { "ctts", ngx_http_mp4_read_ctts_atom },
  400.     { "stsc", ngx_http_mp4_read_stsc_atom },
  401.     { "stsz", ngx_http_mp4_read_stsz_atom },
  402.     { "stco", ngx_http_mp4_read_stco_atom },
  403.     { "co64", ngx_http_mp4_read_co64_atom },
  404.     { NULL, NULL }
  405. };


  406. static ngx_int_t
  407. ngx_http_mp4_handler(ngx_http_request_t *r)
  408. {
  409.     u_char                    *last;
  410.     size_t                     root;
  411.     ngx_int_t                  rc, start, end;
  412.     ngx_uint_t                 level, length;
  413.     ngx_str_t                  path, value;
  414.     ngx_log_t                 *log;
  415.     ngx_buf_t                 *b;
  416.     ngx_chain_t                out;
  417.     ngx_http_mp4_file_t       *mp4;
  418.     ngx_open_file_info_t       of;
  419.     ngx_http_core_loc_conf_t  *clcf;

  420.     if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
  421.         return NGX_HTTP_NOT_ALLOWED;
  422.     }

  423.     if (r->uri.data[r->uri.len - 1] == '/') {
  424.         return NGX_DECLINED;
  425.     }

  426.     rc = ngx_http_discard_request_body(r);

  427.     if (rc != NGX_OK) {
  428.         return rc;
  429.     }

  430.     last = ngx_http_map_uri_to_path(r, &path, &root, 0);
  431.     if (last == NULL) {
  432.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  433.     }

  434.     log = r->connection->log;

  435.     path.len = last - path.data;

  436.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, log, 0,
  437.                    "http mp4 filename: \"%V\"", &path);

  438.     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

  439.     ngx_memzero(&of, sizeof(ngx_open_file_info_t));

  440.     of.read_ahead = clcf->read_ahead;
  441.     of.directio = NGX_MAX_OFF_T_VALUE;
  442.     of.valid = clcf->open_file_cache_valid;
  443.     of.min_uses = clcf->open_file_cache_min_uses;
  444.     of.errors = clcf->open_file_cache_errors;
  445.     of.events = clcf->open_file_cache_events;

  446.     if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK) {
  447.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  448.     }

  449.     if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
  450.         != NGX_OK)
  451.     {
  452.         switch (of.err) {

  453.         case 0:
  454.             return NGX_HTTP_INTERNAL_SERVER_ERROR;

  455.         case NGX_ENOENT:
  456.         case NGX_ENOTDIR:
  457.         case NGX_ENAMETOOLONG:

  458.             level = NGX_LOG_ERR;
  459.             rc = NGX_HTTP_NOT_FOUND;
  460.             break;

  461.         case NGX_EACCES:
  462. #if (NGX_HAVE_OPENAT)
  463.         case NGX_EMLINK:
  464.         case NGX_ELOOP:
  465. #endif

  466.             level = NGX_LOG_ERR;
  467.             rc = NGX_HTTP_FORBIDDEN;
  468.             break;

  469.         default:

  470.             level = NGX_LOG_CRIT;
  471.             rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
  472.             break;
  473.         }

  474.         if (rc != NGX_HTTP_NOT_FOUND || clcf->log_not_found) {
  475.             ngx_log_error(level, log, of.err,
  476.                           "%s \"%s\" failed", of.failed, path.data);
  477.         }

  478.         return rc;
  479.     }

  480.     if (!of.is_file) {
  481.         return NGX_DECLINED;
  482.     }

  483.     r->root_tested = !r->error_page;
  484.     r->allow_ranges = 1;

  485.     start = -1;
  486.     length = 0;
  487.     r->headers_out.content_length_n = of.size;
  488.     mp4 = NULL;
  489.     b = NULL;

  490.     if (r->args.len) {

  491.         if (ngx_http_arg(r, (u_char *) "start", 5, &value) == NGX_OK) {

  492.             /*
  493.              * A Flash player may send start value with a lot of digits
  494.              * after dot so a custom function is used instead of ngx_atofp().
  495.              */

  496.             start = ngx_http_mp4_atofp(value.data, value.len, 3);
  497.         }

  498.         if (ngx_http_arg(r, (u_char *) "end", 3, &value) == NGX_OK) {

  499.             end = ngx_http_mp4_atofp(value.data, value.len, 3);

  500.             if (end > 0) {
  501.                 if (start < 0) {
  502.                     start = 0;
  503.                 }

  504.                 if (end > start) {
  505.                     length = end - start;
  506.                 }
  507.             }
  508.         }
  509.     }

  510.     if (start >= 0) {
  511.         r->single_range = 1;

  512.         mp4 = ngx_pcalloc(r->pool, sizeof(ngx_http_mp4_file_t));
  513.         if (mp4 == NULL) {
  514.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  515.         }

  516.         mp4->file.fd = of.fd;
  517.         mp4->file.name = path;
  518.         mp4->file.log = r->connection->log;
  519.         mp4->end = of.size;
  520.         mp4->start = (ngx_uint_t) start;
  521.         mp4->length = length;
  522.         mp4->request = r;

  523.         switch (ngx_http_mp4_process(mp4)) {

  524.         case NGX_DECLINED:
  525.             if (mp4->buffer) {
  526.                 ngx_pfree(r->pool, mp4->buffer);
  527.             }

  528.             ngx_pfree(r->pool, mp4);
  529.             mp4 = NULL;

  530.             break;

  531.         case NGX_OK:
  532.             r->headers_out.content_length_n = mp4->content_length;
  533.             break;

  534.         default: /* NGX_ERROR */
  535.             if (mp4->buffer) {
  536.                 ngx_pfree(r->pool, mp4->buffer);
  537.             }

  538.             ngx_pfree(r->pool, mp4);

  539.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  540.         }
  541.     }

  542.     log->action = "sending mp4 to client";

  543.     if (clcf->directio <= of.size) {

  544.         /*
  545.          * DIRECTIO is set on transfer only
  546.          * to allow kernel to cache "moov" atom
  547.          */

  548.         if (ngx_directio_on(of.fd) == NGX_FILE_ERROR) {
  549.             ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
  550.                           ngx_directio_on_n " \"%s\" failed", path.data);
  551.         }

  552.         of.is_directio = 1;

  553.         if (mp4) {
  554.             mp4->file.directio = 1;
  555.         }
  556.     }

  557.     r->headers_out.status = NGX_HTTP_OK;
  558.     r->headers_out.last_modified_time = of.mtime;

  559.     if (ngx_http_set_etag(r) != NGX_OK) {
  560.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  561.     }

  562.     if (ngx_http_set_content_type(r) != NGX_OK) {
  563.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  564.     }

  565.     if (mp4 == NULL) {
  566.         b = ngx_calloc_buf(r->pool);
  567.         if (b == NULL) {
  568.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  569.         }

  570.         b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
  571.         if (b->file == NULL) {
  572.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  573.         }
  574.     }

  575.     rc = ngx_http_send_header(r);

  576.     if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
  577.         return rc;
  578.     }

  579.     if (mp4) {
  580.         return ngx_http_output_filter(r, mp4->out);
  581.     }

  582.     b->file_pos = 0;
  583.     b->file_last = of.size;

  584.     b->in_file = b->file_last ? 1 : 0;
  585.     b->last_buf = (r == r->main) ? 1 : 0;
  586.     b->last_in_chain = 1;
  587.     b->sync = (b->last_buf || b->in_file) ? 0 : 1;

  588.     b->file->fd = of.fd;
  589.     b->file->name = path;
  590.     b->file->log = log;
  591.     b->file->directio = of.is_directio;

  592.     out.buf = b;
  593.     out.next = NULL;

  594.     return ngx_http_output_filter(r, &out);
  595. }


  596. static ngx_int_t
  597. ngx_http_mp4_atofp(u_char *line, size_t n, size_t point)
  598. {
  599.     ngx_int_t   value, cutoff, cutlim;
  600.     ngx_uint_t  dot;

  601.     /* same as ngx_atofp(), but allows additional digits */

  602.     if (n == 0) {
  603.         return NGX_ERROR;
  604.     }

  605.     cutoff = NGX_MAX_INT_T_VALUE / 10;
  606.     cutlim = NGX_MAX_INT_T_VALUE % 10;

  607.     dot = 0;

  608.     for (value = 0; n--; line++) {

  609.         if (*line == '.') {
  610.             if (dot) {
  611.                 return NGX_ERROR;
  612.             }

  613.             dot = 1;
  614.             continue;
  615.         }

  616.         if (*line < '0' || *line > '9') {
  617.             return NGX_ERROR;
  618.         }

  619.         if (point == 0) {
  620.             continue;
  621.         }

  622.         if (value >= cutoff && (value > cutoff || *line - '0' > cutlim)) {
  623.             return NGX_ERROR;
  624.         }

  625.         value = value * 10 + (*line - '0');
  626.         point -= dot;
  627.     }

  628.     while (point--) {
  629.         if (value > cutoff) {
  630.             return NGX_ERROR;
  631.         }

  632.         value = value * 10;
  633.     }

  634.     return value;
  635. }


  636. static ngx_int_t
  637. ngx_http_mp4_process(ngx_http_mp4_file_t *mp4)
  638. {
  639.     off_t                  start_offset, end_offset, adjustment;
  640.     ngx_int_t              rc;
  641.     ngx_uint_t             i, j;
  642.     ngx_chain_t          **prev;
  643.     ngx_http_mp4_trak_t   *trak;
  644.     ngx_http_mp4_conf_t   *conf;

  645.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  646.                    "mp4 start:%ui, length:%ui", mp4->start, mp4->length);

  647.     conf = ngx_http_get_module_loc_conf(mp4->request, ngx_http_mp4_module);

  648.     mp4->buffer_size = conf->buffer_size;

  649.     rc = ngx_http_mp4_read_atom(mp4, ngx_http_mp4_atoms, mp4->end);
  650.     if (rc != NGX_OK) {
  651.         return rc;
  652.     }

  653.     if (mp4->trak.nelts == 0) {
  654.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  655.                       "no mp4 trak atoms were found in \"%s\"",
  656.                       mp4->file.name.data);
  657.         return NGX_ERROR;
  658.     }

  659.     if (mp4->mdat_atom.buf == NULL) {
  660.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  661.                       "no mp4 mdat atom was found in \"%s\"",
  662.                       mp4->file.name.data);
  663.         return NGX_ERROR;
  664.     }

  665.     prev = &mp4->out;

  666.     if (mp4->ftyp_atom.buf) {
  667.         *prev = &mp4->ftyp_atom;
  668.         prev = &mp4->ftyp_atom.next;
  669.     }

  670.     *prev = &mp4->moov_atom;
  671.     prev = &mp4->moov_atom.next;

  672.     if (mp4->mvhd_atom.buf) {
  673.         mp4->moov_size += mp4->mvhd_atom_buf.last - mp4->mvhd_atom_buf.pos;
  674.         *prev = &mp4->mvhd_atom;
  675.         prev = &mp4->mvhd_atom.next;
  676.     }

  677.     start_offset = mp4->end;
  678.     end_offset = 0;
  679.     trak = mp4->trak.elts;

  680.     for (i = 0; i < mp4->trak.nelts; i++) {

  681.         if (ngx_http_mp4_update_stts_atom(mp4, &trak[i]) != NGX_OK) {
  682.             return NGX_ERROR;
  683.         }

  684.         if (ngx_http_mp4_update_stss_atom(mp4, &trak[i]) != NGX_OK) {
  685.             return NGX_ERROR;
  686.         }

  687.         ngx_http_mp4_update_ctts_atom(mp4, &trak[i]);

  688.         if (ngx_http_mp4_update_stsc_atom(mp4, &trak[i]) != NGX_OK) {
  689.             return NGX_ERROR;
  690.         }

  691.         if (ngx_http_mp4_update_stsz_atom(mp4, &trak[i]) != NGX_OK) {
  692.             return NGX_ERROR;
  693.         }

  694.         if (trak[i].out[NGX_HTTP_MP4_CO64_DATA].buf) {
  695.             if (ngx_http_mp4_update_co64_atom(mp4, &trak[i]) != NGX_OK) {
  696.                 return NGX_ERROR;
  697.             }

  698.         } else {
  699.             if (ngx_http_mp4_update_stco_atom(mp4, &trak[i]) != NGX_OK) {
  700.                 return NGX_ERROR;
  701.             }
  702.         }

  703.         ngx_http_mp4_update_stbl_atom(mp4, &trak[i]);
  704.         ngx_http_mp4_update_minf_atom(mp4, &trak[i]);
  705.         ngx_http_mp4_update_mdhd_atom(mp4, &trak[i]);
  706.         trak[i].size += trak[i].hdlr_size;
  707.         ngx_http_mp4_update_mdia_atom(mp4, &trak[i]);
  708.         trak[i].size += trak[i].tkhd_size;
  709.         ngx_http_mp4_update_edts_atom(mp4, &trak[i]);
  710.         ngx_http_mp4_update_trak_atom(mp4, &trak[i]);

  711.         mp4->moov_size += trak[i].size;

  712.         if (start_offset > trak[i].start_offset) {
  713.             start_offset = trak[i].start_offset;
  714.         }

  715.         if (end_offset < trak[i].end_offset) {
  716.             end_offset = trak[i].end_offset;
  717.         }

  718.         *prev = &trak[i].out[NGX_HTTP_MP4_TRAK_ATOM];
  719.         prev = &trak[i].out[NGX_HTTP_MP4_TRAK_ATOM].next;

  720.         for (j = 0; j < NGX_HTTP_MP4_LAST_ATOM + 1; j++) {
  721.             if (trak[i].out[j].buf) {
  722.                 *prev = &trak[i].out[j];
  723.                 prev = &trak[i].out[j].next;
  724.             }
  725.         }
  726.     }

  727.     if (end_offset <= start_offset) {
  728.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  729.                       "no data between start time and end time in \"%s\"",
  730.                       mp4->file.name.data);
  731.         return NGX_ERROR;
  732.     }

  733.     mp4->moov_size += 8;

  734.     ngx_mp4_set_32value(mp4->moov_atom_header, mp4->moov_size);
  735.     ngx_mp4_set_atom_name(mp4->moov_atom_header, 'm', 'o', 'o', 'v');
  736.     mp4->content_length += mp4->moov_size;

  737.     *prev = &mp4->mdat_atom;

  738.     if (start_offset >= mp4->mdat_data.buf->file_last) {
  739.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  740.                       "start time is out mp4 mdat atom in \"%s\"",
  741.                       mp4->file.name.data);
  742.         return NGX_ERROR;
  743.     }

  744.     adjustment = mp4->ftyp_size + mp4->moov_size
  745.                  + ngx_http_mp4_update_mdat_atom(mp4, start_offset, end_offset)
  746.                  - start_offset;

  747.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  748.                    "mp4 adjustment:%O", adjustment);

  749.     for (i = 0; i < mp4->trak.nelts; i++) {
  750.         if (trak[i].out[NGX_HTTP_MP4_CO64_DATA].buf) {
  751.             ngx_http_mp4_adjust_co64_atom(mp4, &trak[i], adjustment);
  752.         } else {
  753.             ngx_http_mp4_adjust_stco_atom(mp4, &trak[i], (int32_t) adjustment);
  754.         }
  755.     }

  756.     return NGX_OK;
  757. }


  758. typedef struct {
  759.     u_char    size[4];
  760.     u_char    name[4];
  761. } ngx_mp4_atom_header_t;

  762. typedef struct {
  763.     u_char    size[4];
  764.     u_char    name[4];
  765.     u_char    size64[8];
  766. } ngx_mp4_atom_header64_t;


  767. static ngx_int_t
  768. ngx_http_mp4_read_atom(ngx_http_mp4_file_t *mp4,
  769.     ngx_http_mp4_atom_handler_t *atom, uint64_t atom_data_size)
  770. {
  771.     off_t        end;
  772.     size_t       atom_header_size;
  773.     u_char      *atom_header, *atom_name;
  774.     uint64_t     atom_size;
  775.     ngx_int_t    rc;
  776.     ngx_uint_t   n;

  777.     end = mp4->offset + atom_data_size;

  778.     while (mp4->offset < end) {

  779.         if (ngx_http_mp4_read(mp4, sizeof(uint32_t)) != NGX_OK) {
  780.             return NGX_ERROR;
  781.         }

  782.         atom_header = mp4->buffer_pos;
  783.         atom_size = ngx_mp4_get_32value(atom_header);
  784.         atom_header_size = sizeof(ngx_mp4_atom_header_t);

  785.         if (atom_size == 0) {
  786.             ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  787.                            "mp4 atom end");
  788.             return NGX_OK;
  789.         }

  790.         if (atom_size < sizeof(ngx_mp4_atom_header_t)) {

  791.             if (atom_size == 1) {

  792.                 if (ngx_http_mp4_read(mp4, sizeof(ngx_mp4_atom_header64_t))
  793.                     != NGX_OK)
  794.                 {
  795.                     return NGX_ERROR;
  796.                 }

  797.                 /* 64-bit atom size */
  798.                 atom_header = mp4->buffer_pos;
  799.                 atom_size = ngx_mp4_get_64value(atom_header + 8);
  800.                 atom_header_size = sizeof(ngx_mp4_atom_header64_t);

  801.                 if (atom_size < sizeof(ngx_mp4_atom_header64_t)) {
  802.                     ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  803.                                   "\"%s\" mp4 atom is too small:%uL",
  804.                                   mp4->file.name.data, atom_size);
  805.                     return NGX_ERROR;
  806.                 }

  807.             } else {
  808.                 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  809.                               "\"%s\" mp4 atom is too small:%uL",
  810.                               mp4->file.name.data, atom_size);
  811.                 return NGX_ERROR;
  812.             }
  813.         }

  814.         if (ngx_http_mp4_read(mp4, sizeof(ngx_mp4_atom_header_t)) != NGX_OK) {
  815.             return NGX_ERROR;
  816.         }

  817.         atom_header = mp4->buffer_pos;
  818.         atom_name = atom_header + sizeof(uint32_t);

  819.         ngx_log_debug4(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  820.                        "mp4 atom: %*s @%O:%uL",
  821.                        (size_t) 4, atom_name, mp4->offset, atom_size);

  822.         if (atom_size > (uint64_t) (NGX_MAX_OFF_T_VALUE - mp4->offset)
  823.             || mp4->offset + (off_t) atom_size > end)
  824.         {
  825.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  826.                           "\"%s\" mp4 atom too large:%uL",
  827.                           mp4->file.name.data, atom_size);
  828.             return NGX_ERROR;
  829.         }

  830.         for (n = 0; atom[n].name; n++) {

  831.             if (ngx_strncmp(atom_name, atom[n].name, 4) == 0) {

  832.                 ngx_mp4_atom_next(mp4, atom_header_size);

  833.                 rc = atom[n].handler(mp4, atom_size - atom_header_size);
  834.                 if (rc != NGX_OK) {
  835.                     return rc;
  836.                 }

  837.                 goto next;
  838.             }
  839.         }

  840.         ngx_mp4_atom_next(mp4, atom_size);

  841.     next:
  842.         continue;
  843.     }

  844.     return NGX_OK;
  845. }


  846. static ngx_int_t
  847. ngx_http_mp4_read(ngx_http_mp4_file_t *mp4, size_t size)
  848. {
  849.     ssize_t  n;

  850.     if (mp4->buffer_pos && mp4->buffer_end
  851.         && mp4->buffer_pos + size <= mp4->buffer_end)
  852.     {
  853.         return NGX_OK;
  854.     }

  855.     if (mp4->offset + (off_t) mp4->buffer_size > mp4->end) {
  856.         mp4->buffer_size = (size_t) (mp4->end - mp4->offset);
  857.     }

  858.     if (mp4->buffer_size < size) {
  859.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  860.                       "\"%s\" mp4 file truncated", mp4->file.name.data);
  861.         return NGX_ERROR;
  862.     }

  863.     if (mp4->buffer == NULL) {
  864.         mp4->buffer = ngx_palloc(mp4->request->pool, mp4->buffer_size);
  865.         if (mp4->buffer == NULL) {
  866.             return NGX_ERROR;
  867.         }

  868.         mp4->buffer_start = mp4->buffer;
  869.     }

  870.     n = ngx_read_file(&mp4->file, mp4->buffer_start, mp4->buffer_size,
  871.                       mp4->offset);

  872.     if (n == NGX_ERROR) {
  873.         return NGX_ERROR;
  874.     }

  875.     if ((size_t) n != mp4->buffer_size) {
  876.         ngx_log_error(NGX_LOG_CRIT, mp4->file.log, 0,
  877.                       ngx_read_file_n " read only %z of %z from \"%s\"",
  878.                       n, mp4->buffer_size, mp4->file.name.data);
  879.         return NGX_ERROR;
  880.     }

  881.     mp4->buffer_pos = mp4->buffer_start;
  882.     mp4->buffer_end = mp4->buffer_start + mp4->buffer_size;

  883.     return NGX_OK;
  884. }


  885. static ngx_int_t
  886. ngx_http_mp4_read_ftyp_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  887. {
  888.     u_char     *ftyp_atom;
  889.     size_t      atom_size;
  890.     ngx_buf_t  *atom;

  891.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 ftyp atom");

  892.     if (atom_data_size > 1024
  893.         || ngx_mp4_atom_data(mp4) + (size_t) atom_data_size > mp4->buffer_end)
  894.     {
  895.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  896.                       "\"%s\" mp4 ftyp atom is too large:%uL",
  897.                       mp4->file.name.data, atom_data_size);
  898.         return NGX_ERROR;
  899.     }

  900.     if (mp4->ftyp_atom.buf) {
  901.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  902.                       "duplicate mp4 ftyp atom in \"%s\"", mp4->file.name.data);
  903.         return NGX_ERROR;
  904.     }

  905.     atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;

  906.     ftyp_atom = ngx_palloc(mp4->request->pool, atom_size);
  907.     if (ftyp_atom == NULL) {
  908.         return NGX_ERROR;
  909.     }

  910.     ngx_mp4_set_32value(ftyp_atom, atom_size);
  911.     ngx_mp4_set_atom_name(ftyp_atom, 'f', 't', 'y', 'p');

  912.     /*
  913.      * only moov atom content is guaranteed to be in mp4->buffer
  914.      * during sending response, so ftyp atom content should be copied
  915.      */
  916.     ngx_memcpy(ftyp_atom + sizeof(ngx_mp4_atom_header_t),
  917.                ngx_mp4_atom_data(mp4), (size_t) atom_data_size);

  918.     atom = &mp4->ftyp_atom_buf;
  919.     atom->temporary = 1;
  920.     atom->pos = ftyp_atom;
  921.     atom->last = ftyp_atom + atom_size;

  922.     mp4->ftyp_atom.buf = atom;
  923.     mp4->ftyp_size = atom_size;
  924.     mp4->content_length = atom_size;

  925.     ngx_mp4_atom_next(mp4, atom_data_size);

  926.     return NGX_OK;
  927. }


  928. /*
  929. * Small excess buffer to process atoms after moov atom, mp4->buffer_start
  930. * will be set to this buffer part after moov atom processing.
  931. */
  932. #define NGX_HTTP_MP4_MOOV_BUFFER_EXCESS  (4 * 1024)

  933. static ngx_int_t
  934. ngx_http_mp4_read_moov_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  935. {
  936.     ngx_int_t             rc;
  937.     ngx_uint_t            no_mdat;
  938.     ngx_buf_t            *atom;
  939.     ngx_http_mp4_conf_t  *conf;

  940.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 moov atom");

  941.     no_mdat = (mp4->mdat_atom.buf == NULL);

  942.     if (no_mdat && mp4->start == 0 && mp4->length == 0) {
  943.         /*
  944.          * send original file if moov atom resides before
  945.          * mdat atom and client requests integral file
  946.          */
  947.         return NGX_DECLINED;
  948.     }

  949.     if (mp4->moov_atom.buf) {
  950.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  951.                       "duplicate mp4 moov atom in \"%s\"", mp4->file.name.data);
  952.         return NGX_ERROR;
  953.     }

  954.     conf = ngx_http_get_module_loc_conf(mp4->request, ngx_http_mp4_module);

  955.     if (atom_data_size > mp4->buffer_size) {

  956.         if (atom_data_size > conf->max_buffer_size) {
  957.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  958.                           "\"%s\" mp4 moov atom is too large:%uL, "
  959.                           "you may want to increase mp4_max_buffer_size",
  960.                           mp4->file.name.data, atom_data_size);
  961.             return NGX_ERROR;
  962.         }

  963.         ngx_pfree(mp4->request->pool, mp4->buffer);
  964.         mp4->buffer = NULL;
  965.         mp4->buffer_pos = NULL;
  966.         mp4->buffer_end = NULL;

  967.         mp4->buffer_size = (size_t) atom_data_size
  968.                          + NGX_HTTP_MP4_MOOV_BUFFER_EXCESS * no_mdat;
  969.     }

  970.     if (ngx_http_mp4_read(mp4, (size_t) atom_data_size) != NGX_OK) {
  971.         return NGX_ERROR;
  972.     }

  973.     mp4->trak.elts = &mp4->traks;
  974.     mp4->trak.size = sizeof(ngx_http_mp4_trak_t);
  975.     mp4->trak.nalloc = 2;
  976.     mp4->trak.pool = mp4->request->pool;

  977.     atom = &mp4->moov_atom_buf;
  978.     atom->temporary = 1;
  979.     atom->pos = mp4->moov_atom_header;
  980.     atom->last = mp4->moov_atom_header + 8;

  981.     mp4->moov_atom.buf = &mp4->moov_atom_buf;

  982.     rc = ngx_http_mp4_read_atom(mp4, ngx_http_mp4_moov_atoms, atom_data_size);

  983.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 moov atom done");

  984.     if (no_mdat) {
  985.         mp4->buffer_start = mp4->buffer_pos;
  986.         mp4->buffer_size = NGX_HTTP_MP4_MOOV_BUFFER_EXCESS;

  987.         if (mp4->buffer_start + mp4->buffer_size > mp4->buffer_end) {
  988.             mp4->buffer = NULL;
  989.             mp4->buffer_pos = NULL;
  990.             mp4->buffer_end = NULL;
  991.         }

  992.     } else {
  993.         /* skip atoms after moov atom */
  994.         mp4->offset = mp4->end;
  995.     }

  996.     return rc;
  997. }


  998. static ngx_int_t
  999. ngx_http_mp4_read_mdat_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1000. {
  1001.     ngx_buf_t  *data;

  1002.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 mdat atom");

  1003.     if (mp4->mdat_atom.buf) {
  1004.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1005.                       "duplicate mp4 mdat atom in \"%s\"", mp4->file.name.data);
  1006.         return NGX_ERROR;
  1007.     }

  1008.     data = &mp4->mdat_data_buf;
  1009.     data->file = &mp4->file;
  1010.     data->in_file = 1;
  1011.     data->last_buf = (mp4->request == mp4->request->main) ? 1 : 0;
  1012.     data->last_in_chain = 1;
  1013.     data->file_last = mp4->offset + atom_data_size;

  1014.     mp4->mdat_atom.buf = &mp4->mdat_atom_buf;
  1015.     mp4->mdat_atom.next = &mp4->mdat_data;
  1016.     mp4->mdat_data.buf = data;

  1017.     if (mp4->trak.nelts) {
  1018.         /* skip atoms after mdat atom */
  1019.         mp4->offset = mp4->end;

  1020.     } else {
  1021.         ngx_mp4_atom_next(mp4, atom_data_size);
  1022.     }

  1023.     return NGX_OK;
  1024. }


  1025. static size_t
  1026. ngx_http_mp4_update_mdat_atom(ngx_http_mp4_file_t *mp4, off_t start_offset,
  1027.     off_t end_offset)
  1028. {
  1029.     off_t       atom_data_size;
  1030.     u_char     *atom_header;
  1031.     uint32_t    atom_header_size;
  1032.     uint64_t    atom_size;
  1033.     ngx_buf_t  *atom;

  1034.     atom_data_size = end_offset - start_offset;
  1035.     mp4->mdat_data.buf->file_pos = start_offset;
  1036.     mp4->mdat_data.buf->file_last = end_offset;

  1037.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1038.                    "mdat new offset @%O:%O", start_offset, atom_data_size);

  1039.     atom_header = mp4->mdat_atom_header;

  1040.     if ((uint64_t) atom_data_size
  1041.         > (uint64_t) 0xffffffff - sizeof(ngx_mp4_atom_header_t))
  1042.     {
  1043.         atom_size = 1;
  1044.         atom_header_size = sizeof(ngx_mp4_atom_header64_t);
  1045.         ngx_mp4_set_64value(atom_header + sizeof(ngx_mp4_atom_header_t),
  1046.                             sizeof(ngx_mp4_atom_header64_t) + atom_data_size);
  1047.     } else {
  1048.         atom_size = sizeof(ngx_mp4_atom_header_t) + atom_data_size;
  1049.         atom_header_size = sizeof(ngx_mp4_atom_header_t);
  1050.     }

  1051.     mp4->content_length += atom_header_size + atom_data_size;

  1052.     ngx_mp4_set_32value(atom_header, atom_size);
  1053.     ngx_mp4_set_atom_name(atom_header, 'm', 'd', 'a', 't');

  1054.     atom = &mp4->mdat_atom_buf;
  1055.     atom->temporary = 1;
  1056.     atom->pos = atom_header;
  1057.     atom->last = atom_header + atom_header_size;

  1058.     return atom_header_size;
  1059. }


  1060. typedef struct {
  1061.     u_char    size[4];
  1062.     u_char    name[4];
  1063.     u_char    version[1];
  1064.     u_char    flags[3];
  1065.     u_char    creation_time[4];
  1066.     u_char    modification_time[4];
  1067.     u_char    timescale[4];
  1068.     u_char    duration[4];
  1069.     u_char    rate[4];
  1070.     u_char    volume[2];
  1071.     u_char    reserved[10];
  1072.     u_char    matrix[36];
  1073.     u_char    preview_time[4];
  1074.     u_char    preview_duration[4];
  1075.     u_char    poster_time[4];
  1076.     u_char    selection_time[4];
  1077.     u_char    selection_duration[4];
  1078.     u_char    current_time[4];
  1079.     u_char    next_track_id[4];
  1080. } ngx_mp4_mvhd_atom_t;

  1081. typedef struct {
  1082.     u_char    size[4];
  1083.     u_char    name[4];
  1084.     u_char    version[1];
  1085.     u_char    flags[3];
  1086.     u_char    creation_time[8];
  1087.     u_char    modification_time[8];
  1088.     u_char    timescale[4];
  1089.     u_char    duration[8];
  1090.     u_char    rate[4];
  1091.     u_char    volume[2];
  1092.     u_char    reserved[10];
  1093.     u_char    matrix[36];
  1094.     u_char    preview_time[4];
  1095.     u_char    preview_duration[4];
  1096.     u_char    poster_time[4];
  1097.     u_char    selection_time[4];
  1098.     u_char    selection_duration[4];
  1099.     u_char    current_time[4];
  1100.     u_char    next_track_id[4];
  1101. } ngx_mp4_mvhd64_atom_t;


  1102. static ngx_int_t
  1103. ngx_http_mp4_read_mvhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1104. {
  1105.     u_char                 *atom_header;
  1106.     size_t                  atom_size;
  1107.     uint32_t                timescale;
  1108.     uint64_t                duration, start_time, length_time;
  1109.     ngx_buf_t              *atom;
  1110.     ngx_mp4_mvhd_atom_t    *mvhd_atom;
  1111.     ngx_mp4_mvhd64_atom_t  *mvhd64_atom;

  1112.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 mvhd atom");

  1113.     if (mp4->mvhd_atom.buf) {
  1114.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1115.                       "duplicate mp4 mvhd atom in \"%s\"", mp4->file.name.data);
  1116.         return NGX_ERROR;
  1117.     }

  1118.     atom_header = ngx_mp4_atom_header(mp4);
  1119.     mvhd_atom = (ngx_mp4_mvhd_atom_t *) atom_header;
  1120.     mvhd64_atom = (ngx_mp4_mvhd64_atom_t *) atom_header;
  1121.     ngx_mp4_set_atom_name(atom_header, 'm', 'v', 'h', 'd');

  1122.     if (ngx_mp4_atom_data_size(ngx_mp4_mvhd_atom_t) > atom_data_size) {
  1123.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1124.                       "\"%s\" mp4 mvhd atom too small", mp4->file.name.data);
  1125.         return NGX_ERROR;
  1126.     }

  1127.     if (mvhd_atom->version[0] == 0) {
  1128.         /* version 0: 32-bit duration */
  1129.         timescale = ngx_mp4_get_32value(mvhd_atom->timescale);
  1130.         duration = ngx_mp4_get_32value(mvhd_atom->duration);

  1131.     } else {
  1132.         /* version 1: 64-bit duration */

  1133.         if (ngx_mp4_atom_data_size(ngx_mp4_mvhd64_atom_t) > atom_data_size) {
  1134.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1135.                           "\"%s\" mp4 mvhd atom too small",
  1136.                           mp4->file.name.data);
  1137.             return NGX_ERROR;
  1138.         }

  1139.         timescale = ngx_mp4_get_32value(mvhd64_atom->timescale);
  1140.         duration = ngx_mp4_get_64value(mvhd64_atom->duration);
  1141.     }

  1142.     mp4->timescale = timescale;

  1143.     ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1144.                    "mvhd timescale:%uD, duration:%uL, time:%.3fs",
  1145.                    timescale, duration, (double) duration / timescale);

  1146.     start_time = (uint64_t) mp4->start * timescale / 1000;

  1147.     if (duration < start_time) {
  1148.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1149.                       "\"%s\" mp4 start time exceeds file duration",
  1150.                       mp4->file.name.data);
  1151.         return NGX_ERROR;
  1152.     }

  1153.     duration -= start_time;

  1154.     if (mp4->length) {
  1155.         length_time = (uint64_t) mp4->length * timescale / 1000;

  1156.         if (duration > length_time) {
  1157.             duration = length_time;
  1158.         }
  1159.     }

  1160.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1161.                    "mvhd new duration:%uL, time:%.3fs",
  1162.                    duration, (double) duration / timescale);

  1163.     atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
  1164.     ngx_mp4_set_32value(mvhd_atom->size, atom_size);

  1165.     if (mvhd_atom->version[0] == 0) {
  1166.         ngx_mp4_set_32value(mvhd_atom->duration, duration);

  1167.     } else {
  1168.         ngx_mp4_set_64value(mvhd64_atom->duration, duration);
  1169.     }

  1170.     atom = &mp4->mvhd_atom_buf;
  1171.     atom->temporary = 1;
  1172.     atom->pos = atom_header;
  1173.     atom->last = atom_header + atom_size;

  1174.     mp4->mvhd_atom.buf = atom;

  1175.     ngx_mp4_atom_next(mp4, atom_data_size);

  1176.     return NGX_OK;
  1177. }


  1178. static ngx_int_t
  1179. ngx_http_mp4_read_trak_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1180. {
  1181.     u_char               *atom_header, *atom_end;
  1182.     off_t                 atom_file_end;
  1183.     ngx_int_t             rc;
  1184.     ngx_buf_t            *atom;
  1185.     ngx_http_mp4_trak_t  *trak;

  1186.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 trak atom");

  1187.     trak = ngx_array_push(&mp4->trak);
  1188.     if (trak == NULL) {
  1189.         return NGX_ERROR;
  1190.     }

  1191.     ngx_memzero(trak, sizeof(ngx_http_mp4_trak_t));

  1192.     atom_header = ngx_mp4_atom_header(mp4);
  1193.     ngx_mp4_set_atom_name(atom_header, 't', 'r', 'a', 'k');

  1194.     atom = &trak->trak_atom_buf;
  1195.     atom->temporary = 1;
  1196.     atom->pos = atom_header;
  1197.     atom->last = atom_header + sizeof(ngx_mp4_atom_header_t);

  1198.     trak->out[NGX_HTTP_MP4_TRAK_ATOM].buf = atom;

  1199.     atom_end = mp4->buffer_pos + (size_t) atom_data_size;
  1200.     atom_file_end = mp4->offset + atom_data_size;

  1201.     rc = ngx_http_mp4_read_atom(mp4, ngx_http_mp4_trak_atoms, atom_data_size);

  1202.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1203.                    "mp4 trak atom: %i", rc);

  1204.     if (rc == NGX_DECLINED) {
  1205.         /* skip this trak */
  1206.         ngx_memzero(trak, sizeof(ngx_http_mp4_trak_t));
  1207.         mp4->trak.nelts--;
  1208.         mp4->buffer_pos = atom_end;
  1209.         mp4->offset = atom_file_end;
  1210.         return NGX_OK;
  1211.     }

  1212.     return rc;
  1213. }


  1214. static void
  1215. ngx_http_mp4_update_trak_atom(ngx_http_mp4_file_t *mp4,
  1216.     ngx_http_mp4_trak_t *trak)
  1217. {
  1218.     ngx_buf_t  *atom;

  1219.     trak->size += sizeof(ngx_mp4_atom_header_t);
  1220.     atom = &trak->trak_atom_buf;
  1221.     ngx_mp4_set_32value(atom->pos, trak->size);
  1222. }


  1223. static ngx_int_t
  1224. ngx_http_mp4_read_cmov_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1225. {
  1226.     ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1227.                   "\"%s\" mp4 compressed moov atom (cmov) is not supported",
  1228.                   mp4->file.name.data);

  1229.     return NGX_ERROR;
  1230. }


  1231. typedef struct {
  1232.     u_char    size[4];
  1233.     u_char    name[4];
  1234.     u_char    version[1];
  1235.     u_char    flags[3];
  1236.     u_char    creation_time[4];
  1237.     u_char    modification_time[4];
  1238.     u_char    track_id[4];
  1239.     u_char    reserved1[4];
  1240.     u_char    duration[4];
  1241.     u_char    reserved2[8];
  1242.     u_char    layer[2];
  1243.     u_char    group[2];
  1244.     u_char    volume[2];
  1245.     u_char    reserved3[2];
  1246.     u_char    matrix[36];
  1247.     u_char    width[4];
  1248.     u_char    height[4];
  1249. } ngx_mp4_tkhd_atom_t;

  1250. typedef struct {
  1251.     u_char    size[4];
  1252.     u_char    name[4];
  1253.     u_char    version[1];
  1254.     u_char    flags[3];
  1255.     u_char    creation_time[8];
  1256.     u_char    modification_time[8];
  1257.     u_char    track_id[4];
  1258.     u_char    reserved1[4];
  1259.     u_char    duration[8];
  1260.     u_char    reserved2[8];
  1261.     u_char    layer[2];
  1262.     u_char    group[2];
  1263.     u_char    volume[2];
  1264.     u_char    reserved3[2];
  1265.     u_char    matrix[36];
  1266.     u_char    width[4];
  1267.     u_char    height[4];
  1268. } ngx_mp4_tkhd64_atom_t;


  1269. static ngx_int_t
  1270. ngx_http_mp4_read_tkhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1271. {
  1272.     u_char                 *atom_header;
  1273.     size_t                  atom_size;
  1274.     uint64_t                duration, start_time, length_time;
  1275.     ngx_buf_t              *atom;
  1276.     ngx_http_mp4_trak_t    *trak;
  1277.     ngx_mp4_tkhd_atom_t    *tkhd_atom;
  1278.     ngx_mp4_tkhd64_atom_t  *tkhd64_atom;

  1279.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 tkhd atom");

  1280.     atom_header = ngx_mp4_atom_header(mp4);
  1281.     tkhd_atom = (ngx_mp4_tkhd_atom_t *) atom_header;
  1282.     tkhd64_atom = (ngx_mp4_tkhd64_atom_t *) atom_header;
  1283.     ngx_mp4_set_atom_name(tkhd_atom, 't', 'k', 'h', 'd');

  1284.     if (ngx_mp4_atom_data_size(ngx_mp4_tkhd_atom_t) > atom_data_size) {
  1285.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1286.                       "\"%s\" mp4 tkhd atom too small", mp4->file.name.data);
  1287.         return NGX_ERROR;
  1288.     }

  1289.     if (tkhd_atom->version[0] == 0) {
  1290.         /* version 0: 32-bit duration */
  1291.         duration = ngx_mp4_get_32value(tkhd_atom->duration);

  1292.     } else {
  1293.         /* version 1: 64-bit duration */

  1294.         if (ngx_mp4_atom_data_size(ngx_mp4_tkhd64_atom_t) > atom_data_size) {
  1295.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1296.                           "\"%s\" mp4 tkhd atom too small",
  1297.                           mp4->file.name.data);
  1298.             return NGX_ERROR;
  1299.         }

  1300.         duration = ngx_mp4_get_64value(tkhd64_atom->duration);
  1301.     }

  1302.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1303.                    "tkhd duration:%uL, time:%.3fs",
  1304.                    duration, (double) duration / mp4->timescale);

  1305.     start_time = (uint64_t) mp4->start * mp4->timescale / 1000;

  1306.     if (duration <= start_time) {
  1307.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1308.                        "tkhd duration is less than start time");
  1309.         return NGX_DECLINED;
  1310.     }

  1311.     duration -= start_time;

  1312.     if (mp4->length) {
  1313.         length_time = (uint64_t) mp4->length * mp4->timescale / 1000;

  1314.         if (duration > length_time) {
  1315.             duration = length_time;
  1316.         }
  1317.     }

  1318.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1319.                    "tkhd new duration:%uL, time:%.3fs",
  1320.                    duration, (double) duration / mp4->timescale);

  1321.     atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;

  1322.     trak = ngx_mp4_last_trak(mp4);

  1323.     if (trak->out[NGX_HTTP_MP4_TKHD_ATOM].buf) {
  1324.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1325.                       "duplicate mp4 tkhd atom in \"%s\"", mp4->file.name.data);
  1326.         return NGX_ERROR;
  1327.     }

  1328.     trak->tkhd_size = atom_size;
  1329.     trak->movie_duration = duration;

  1330.     ngx_mp4_set_32value(tkhd_atom->size, atom_size);

  1331.     if (tkhd_atom->version[0] == 0) {
  1332.         ngx_mp4_set_32value(tkhd_atom->duration, duration);

  1333.     } else {
  1334.         ngx_mp4_set_64value(tkhd64_atom->duration, duration);
  1335.     }

  1336.     atom = &trak->tkhd_atom_buf;
  1337.     atom->temporary = 1;
  1338.     atom->pos = atom_header;
  1339.     atom->last = atom_header + atom_size;

  1340.     trak->out[NGX_HTTP_MP4_TKHD_ATOM].buf = atom;

  1341.     ngx_mp4_atom_next(mp4, atom_data_size);

  1342.     return NGX_OK;
  1343. }


  1344. static ngx_int_t
  1345. ngx_http_mp4_read_mdia_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1346. {
  1347.     u_char               *atom_header;
  1348.     ngx_buf_t            *atom;
  1349.     ngx_http_mp4_trak_t  *trak;

  1350.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "process mdia atom");

  1351.     atom_header = ngx_mp4_atom_header(mp4);
  1352.     ngx_mp4_set_atom_name(atom_header, 'm', 'd', 'i', 'a');

  1353.     trak = ngx_mp4_last_trak(mp4);

  1354.     if (trak->out[NGX_HTTP_MP4_MDIA_ATOM].buf) {
  1355.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1356.                       "duplicate mp4 mdia atom in \"%s\"", mp4->file.name.data);
  1357.         return NGX_ERROR;
  1358.     }

  1359.     atom = &trak->mdia_atom_buf;
  1360.     atom->temporary = 1;
  1361.     atom->pos = atom_header;
  1362.     atom->last = atom_header + sizeof(ngx_mp4_atom_header_t);

  1363.     trak->out[NGX_HTTP_MP4_MDIA_ATOM].buf = atom;

  1364.     return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_mdia_atoms, atom_data_size);
  1365. }


  1366. static void
  1367. ngx_http_mp4_update_mdia_atom(ngx_http_mp4_file_t *mp4,
  1368.     ngx_http_mp4_trak_t *trak)
  1369. {
  1370.     ngx_buf_t  *atom;

  1371.     trak->size += sizeof(ngx_mp4_atom_header_t);
  1372.     atom = &trak->mdia_atom_buf;
  1373.     ngx_mp4_set_32value(atom->pos, trak->size);
  1374. }


  1375. typedef struct {
  1376.     u_char    size[4];
  1377.     u_char    name[4];
  1378.     u_char    version[1];
  1379.     u_char    flags[3];
  1380.     u_char    creation_time[4];
  1381.     u_char    modification_time[4];
  1382.     u_char    timescale[4];
  1383.     u_char    duration[4];
  1384.     u_char    language[2];
  1385.     u_char    quality[2];
  1386. } ngx_mp4_mdhd_atom_t;

  1387. typedef struct {
  1388.     u_char    size[4];
  1389.     u_char    name[4];
  1390.     u_char    version[1];
  1391.     u_char    flags[3];
  1392.     u_char    creation_time[8];
  1393.     u_char    modification_time[8];
  1394.     u_char    timescale[4];
  1395.     u_char    duration[8];
  1396.     u_char    language[2];
  1397.     u_char    quality[2];
  1398. } ngx_mp4_mdhd64_atom_t;


  1399. static ngx_int_t
  1400. ngx_http_mp4_read_mdhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1401. {
  1402.     u_char                 *atom_header;
  1403.     size_t                  atom_size;
  1404.     uint32_t                timescale;
  1405.     uint64_t                duration, start_time, length_time;
  1406.     ngx_buf_t              *atom;
  1407.     ngx_http_mp4_trak_t    *trak;
  1408.     ngx_mp4_mdhd_atom_t    *mdhd_atom;
  1409.     ngx_mp4_mdhd64_atom_t  *mdhd64_atom;

  1410.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 mdhd atom");

  1411.     atom_header = ngx_mp4_atom_header(mp4);
  1412.     mdhd_atom = (ngx_mp4_mdhd_atom_t *) atom_header;
  1413.     mdhd64_atom = (ngx_mp4_mdhd64_atom_t *) atom_header;
  1414.     ngx_mp4_set_atom_name(mdhd_atom, 'm', 'd', 'h', 'd');

  1415.     if (ngx_mp4_atom_data_size(ngx_mp4_mdhd_atom_t) > atom_data_size) {
  1416.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1417.                       "\"%s\" mp4 mdhd atom too small", mp4->file.name.data);
  1418.         return NGX_ERROR;
  1419.     }

  1420.     if (mdhd_atom->version[0] == 0) {
  1421.         /* version 0: everything is 32-bit */
  1422.         timescale = ngx_mp4_get_32value(mdhd_atom->timescale);
  1423.         duration = ngx_mp4_get_32value(mdhd_atom->duration);

  1424.     } else {
  1425.         /* version 1: 64-bit duration and 32-bit timescale */

  1426.         if (ngx_mp4_atom_data_size(ngx_mp4_mdhd64_atom_t) > atom_data_size) {
  1427.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1428.                           "\"%s\" mp4 mdhd atom too small",
  1429.                           mp4->file.name.data);
  1430.             return NGX_ERROR;
  1431.         }

  1432.         timescale = ngx_mp4_get_32value(mdhd64_atom->timescale);
  1433.         duration = ngx_mp4_get_64value(mdhd64_atom->duration);
  1434.     }

  1435.     ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1436.                    "mdhd timescale:%uD, duration:%uL, time:%.3fs",
  1437.                    timescale, duration, (double) duration / timescale);

  1438.     start_time = (uint64_t) mp4->start * timescale / 1000;

  1439.     if (duration <= start_time) {
  1440.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1441.                        "mdhd duration is less than start time");
  1442.         return NGX_DECLINED;
  1443.     }

  1444.     duration -= start_time;

  1445.     if (mp4->length) {
  1446.         length_time = (uint64_t) mp4->length * timescale / 1000;

  1447.         if (duration > length_time) {
  1448.             duration = length_time;
  1449.         }
  1450.     }

  1451.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1452.                    "mdhd new duration:%uL, time:%.3fs",
  1453.                    duration, (double) duration / timescale);

  1454.     atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;

  1455.     trak = ngx_mp4_last_trak(mp4);

  1456.     if (trak->out[NGX_HTTP_MP4_MDHD_ATOM].buf) {
  1457.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1458.                       "duplicate mp4 mdhd atom in \"%s\"", mp4->file.name.data);
  1459.         return NGX_ERROR;
  1460.     }

  1461.     trak->mdhd_size = atom_size;
  1462.     trak->timescale = timescale;
  1463.     trak->duration = duration;

  1464.     ngx_mp4_set_32value(mdhd_atom->size, atom_size);

  1465.     atom = &trak->mdhd_atom_buf;
  1466.     atom->temporary = 1;
  1467.     atom->pos = atom_header;
  1468.     atom->last = atom_header + atom_size;

  1469.     trak->out[NGX_HTTP_MP4_MDHD_ATOM].buf = atom;

  1470.     ngx_mp4_atom_next(mp4, atom_data_size);

  1471.     return NGX_OK;
  1472. }


  1473. static void
  1474. ngx_http_mp4_update_mdhd_atom(ngx_http_mp4_file_t *mp4,
  1475.             ngx_http_mp4_trak_t *trak)
  1476. {
  1477.     ngx_buf_t              *atom;
  1478.     ngx_mp4_mdhd_atom_t    *mdhd_atom;
  1479.     ngx_mp4_mdhd64_atom_t  *mdhd64_atom;

  1480.     atom = trak->out[NGX_HTTP_MP4_MDHD_ATOM].buf;
  1481.     if (atom == NULL) {
  1482.         return;
  1483.     }

  1484.     mdhd_atom = (ngx_mp4_mdhd_atom_t *) atom->pos;
  1485.     mdhd64_atom = (ngx_mp4_mdhd64_atom_t *) atom->pos;

  1486.     if (mdhd_atom->version[0] == 0) {
  1487.         ngx_mp4_set_32value(mdhd_atom->duration, trak->duration);

  1488.     } else {
  1489.         ngx_mp4_set_64value(mdhd64_atom->duration, trak->duration);
  1490.     }

  1491.     trak->size += trak->mdhd_size;
  1492. }


  1493. static ngx_int_t
  1494. ngx_http_mp4_read_hdlr_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1495. {
  1496.     u_char              *atom_header;
  1497.     size_t               atom_size;
  1498.     ngx_buf_t            *atom;
  1499.     ngx_http_mp4_trak_t  *trak;

  1500.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 hdlr atom");

  1501.     atom_header = ngx_mp4_atom_header(mp4);
  1502.     atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
  1503.     ngx_mp4_set_32value(atom_header, atom_size);
  1504.     ngx_mp4_set_atom_name(atom_header, 'h', 'd', 'l', 'r');

  1505.     trak = ngx_mp4_last_trak(mp4);

  1506.     if (trak->out[NGX_HTTP_MP4_HDLR_ATOM].buf) {
  1507.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1508.                       "duplicate mp4 hdlr atom in \"%s\"", mp4->file.name.data);
  1509.         return NGX_ERROR;
  1510.     }

  1511.     atom = &trak->hdlr_atom_buf;
  1512.     atom->temporary = 1;
  1513.     atom->pos = atom_header;
  1514.     atom->last = atom_header + atom_size;

  1515.     trak->hdlr_size = atom_size;
  1516.     trak->out[NGX_HTTP_MP4_HDLR_ATOM].buf = atom;

  1517.     ngx_mp4_atom_next(mp4, atom_data_size);

  1518.     return NGX_OK;
  1519. }


  1520. static ngx_int_t
  1521. ngx_http_mp4_read_minf_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1522. {
  1523.     u_char               *atom_header;
  1524.     ngx_buf_t            *atom;
  1525.     ngx_http_mp4_trak_t  *trak;

  1526.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "process minf atom");

  1527.     atom_header = ngx_mp4_atom_header(mp4);
  1528.     ngx_mp4_set_atom_name(atom_header, 'm', 'i', 'n', 'f');

  1529.     trak = ngx_mp4_last_trak(mp4);

  1530.     if (trak->out[NGX_HTTP_MP4_MINF_ATOM].buf) {
  1531.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1532.                       "duplicate mp4 minf atom in \"%s\"", mp4->file.name.data);
  1533.         return NGX_ERROR;
  1534.     }

  1535.     atom = &trak->minf_atom_buf;
  1536.     atom->temporary = 1;
  1537.     atom->pos = atom_header;
  1538.     atom->last = atom_header + sizeof(ngx_mp4_atom_header_t);

  1539.     trak->out[NGX_HTTP_MP4_MINF_ATOM].buf = atom;

  1540.     return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_minf_atoms, atom_data_size);
  1541. }


  1542. static void
  1543. ngx_http_mp4_update_minf_atom(ngx_http_mp4_file_t *mp4,
  1544.     ngx_http_mp4_trak_t *trak)
  1545. {
  1546.     ngx_buf_t  *atom;

  1547.     trak->size += sizeof(ngx_mp4_atom_header_t)
  1548.                + trak->vmhd_size
  1549.                + trak->smhd_size
  1550.                + trak->dinf_size;
  1551.     atom = &trak->minf_atom_buf;
  1552.     ngx_mp4_set_32value(atom->pos, trak->size);
  1553. }


  1554. static ngx_int_t
  1555. ngx_http_mp4_read_vmhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1556. {
  1557.     u_char              *atom_header;
  1558.     size_t               atom_size;
  1559.     ngx_buf_t            *atom;
  1560.     ngx_http_mp4_trak_t  *trak;

  1561.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 vmhd atom");

  1562.     atom_header = ngx_mp4_atom_header(mp4);
  1563.     atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
  1564.     ngx_mp4_set_32value(atom_header, atom_size);
  1565.     ngx_mp4_set_atom_name(atom_header, 'v', 'm', 'h', 'd');

  1566.     trak = ngx_mp4_last_trak(mp4);

  1567.     if (trak->out[NGX_HTTP_MP4_VMHD_ATOM].buf
  1568.         || trak->out[NGX_HTTP_MP4_SMHD_ATOM].buf)
  1569.     {
  1570.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1571.                       "duplicate mp4 vmhd/smhd atom in \"%s\"",
  1572.                       mp4->file.name.data);
  1573.         return NGX_ERROR;
  1574.     }

  1575.     atom = &trak->vmhd_atom_buf;
  1576.     atom->temporary = 1;
  1577.     atom->pos = atom_header;
  1578.     atom->last = atom_header + atom_size;

  1579.     trak->vmhd_size += atom_size;
  1580.     trak->out[NGX_HTTP_MP4_VMHD_ATOM].buf = atom;

  1581.     ngx_mp4_atom_next(mp4, atom_data_size);

  1582.     return NGX_OK;
  1583. }


  1584. static ngx_int_t
  1585. ngx_http_mp4_read_smhd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1586. {
  1587.     u_char              *atom_header;
  1588.     size_t               atom_size;
  1589.     ngx_buf_t            *atom;
  1590.     ngx_http_mp4_trak_t  *trak;

  1591.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 smhd atom");

  1592.     atom_header = ngx_mp4_atom_header(mp4);
  1593.     atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
  1594.     ngx_mp4_set_32value(atom_header, atom_size);
  1595.     ngx_mp4_set_atom_name(atom_header, 's', 'm', 'h', 'd');

  1596.     trak = ngx_mp4_last_trak(mp4);

  1597.     if (trak->out[NGX_HTTP_MP4_VMHD_ATOM].buf
  1598.         || trak->out[NGX_HTTP_MP4_SMHD_ATOM].buf)
  1599.     {
  1600.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1601.                       "duplicate mp4 vmhd/smhd atom in \"%s\"",
  1602.                       mp4->file.name.data);
  1603.         return NGX_ERROR;
  1604.     }

  1605.     atom = &trak->smhd_atom_buf;
  1606.     atom->temporary = 1;
  1607.     atom->pos = atom_header;
  1608.     atom->last = atom_header + atom_size;

  1609.     trak->smhd_size += atom_size;
  1610.     trak->out[NGX_HTTP_MP4_SMHD_ATOM].buf = atom;

  1611.     ngx_mp4_atom_next(mp4, atom_data_size);

  1612.     return NGX_OK;
  1613. }


  1614. static ngx_int_t
  1615. ngx_http_mp4_read_dinf_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1616. {
  1617.     u_char              *atom_header;
  1618.     size_t               atom_size;
  1619.     ngx_buf_t            *atom;
  1620.     ngx_http_mp4_trak_t  *trak;

  1621.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 dinf atom");

  1622.     atom_header = ngx_mp4_atom_header(mp4);
  1623.     atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
  1624.     ngx_mp4_set_32value(atom_header, atom_size);
  1625.     ngx_mp4_set_atom_name(atom_header, 'd', 'i', 'n', 'f');

  1626.     trak = ngx_mp4_last_trak(mp4);

  1627.     if (trak->out[NGX_HTTP_MP4_DINF_ATOM].buf) {
  1628.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1629.                       "duplicate mp4 dinf atom in \"%s\"", mp4->file.name.data);
  1630.         return NGX_ERROR;
  1631.     }

  1632.     atom = &trak->dinf_atom_buf;
  1633.     atom->temporary = 1;
  1634.     atom->pos = atom_header;
  1635.     atom->last = atom_header + atom_size;

  1636.     trak->dinf_size += atom_size;
  1637.     trak->out[NGX_HTTP_MP4_DINF_ATOM].buf = atom;

  1638.     ngx_mp4_atom_next(mp4, atom_data_size);

  1639.     return NGX_OK;
  1640. }


  1641. static ngx_int_t
  1642. ngx_http_mp4_read_stbl_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1643. {
  1644.     u_char               *atom_header;
  1645.     ngx_buf_t            *atom;
  1646.     ngx_http_mp4_trak_t  *trak;

  1647.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "process stbl atom");

  1648.     atom_header = ngx_mp4_atom_header(mp4);
  1649.     ngx_mp4_set_atom_name(atom_header, 's', 't', 'b', 'l');

  1650.     trak = ngx_mp4_last_trak(mp4);

  1651.     if (trak->out[NGX_HTTP_MP4_STBL_ATOM].buf) {
  1652.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1653.                       "duplicate mp4 stbl atom in \"%s\"", mp4->file.name.data);
  1654.         return NGX_ERROR;
  1655.     }

  1656.     atom = &trak->stbl_atom_buf;
  1657.     atom->temporary = 1;
  1658.     atom->pos = atom_header;
  1659.     atom->last = atom_header + sizeof(ngx_mp4_atom_header_t);

  1660.     trak->out[NGX_HTTP_MP4_STBL_ATOM].buf = atom;

  1661.     return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_stbl_atoms, atom_data_size);
  1662. }


  1663. static void
  1664. ngx_http_mp4_update_edts_atom(ngx_http_mp4_file_t *mp4,
  1665.     ngx_http_mp4_trak_t *trak)
  1666. {
  1667.     ngx_buf_t            *atom;
  1668.     ngx_mp4_elst_atom_t  *elst_atom;
  1669.     ngx_mp4_edts_atom_t  *edts_atom;

  1670.     if (trak->prefix == 0) {
  1671.         return;
  1672.     }

  1673.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1674.                    "mp4 edts atom update prefix:%uL", trak->prefix);

  1675.     edts_atom = &trak->edts_atom;
  1676.     ngx_mp4_set_32value(edts_atom->size, sizeof(ngx_mp4_edts_atom_t)
  1677.                                          + sizeof(ngx_mp4_elst_atom_t));
  1678.     ngx_mp4_set_atom_name(edts_atom, 'e', 'd', 't', 's');

  1679.     atom = &trak->edts_atom_buf;
  1680.     atom->temporary = 1;
  1681.     atom->pos = (u_char *) edts_atom;
  1682.     atom->last = (u_char *) edts_atom + sizeof(ngx_mp4_edts_atom_t);

  1683.     trak->out[NGX_HTTP_MP4_EDTS_ATOM].buf = atom;

  1684.     elst_atom = &trak->elst_atom;
  1685.     ngx_mp4_set_32value(elst_atom->size, sizeof(ngx_mp4_elst_atom_t));
  1686.     ngx_mp4_set_atom_name(elst_atom, 'e', 'l', 's', 't');

  1687.     elst_atom->version[0] = 1;
  1688.     elst_atom->flags[0] = 0;
  1689.     elst_atom->flags[1] = 0;
  1690.     elst_atom->flags[2] = 0;

  1691.     ngx_mp4_set_32value(elst_atom->entries, 1);
  1692.     ngx_mp4_set_64value(elst_atom->duration, trak->movie_duration);
  1693.     ngx_mp4_set_64value(elst_atom->media_time, trak->prefix);
  1694.     ngx_mp4_set_16value(elst_atom->media_rate, 1);
  1695.     ngx_mp4_set_16value(elst_atom->reserved, 0);

  1696.     atom = &trak->elst_atom_buf;
  1697.     atom->temporary = 1;
  1698.     atom->pos = (u_char *) elst_atom;
  1699.     atom->last = (u_char *) elst_atom + sizeof(ngx_mp4_elst_atom_t);

  1700.     trak->out[NGX_HTTP_MP4_ELST_ATOM].buf = atom;

  1701.     trak->size += sizeof(ngx_mp4_edts_atom_t) + sizeof(ngx_mp4_elst_atom_t);
  1702. }


  1703. static void
  1704. ngx_http_mp4_update_stbl_atom(ngx_http_mp4_file_t *mp4,
  1705.     ngx_http_mp4_trak_t *trak)
  1706. {
  1707.     ngx_buf_t  *atom;

  1708.     trak->size += sizeof(ngx_mp4_atom_header_t);
  1709.     atom = &trak->stbl_atom_buf;
  1710.     ngx_mp4_set_32value(atom->pos, trak->size);
  1711. }


  1712. typedef struct {
  1713.     u_char    size[4];
  1714.     u_char    name[4];
  1715.     u_char    version[1];
  1716.     u_char    flags[3];
  1717.     u_char    entries[4];

  1718.     u_char    media_size[4];
  1719.     u_char    media_name[4];
  1720. } ngx_mp4_stsd_atom_t;


  1721. static ngx_int_t
  1722. ngx_http_mp4_read_stsd_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1723. {
  1724.     u_char               *atom_header, *atom_table;
  1725.     size_t                atom_size;
  1726.     ngx_buf_t            *atom;
  1727.     ngx_mp4_stsd_atom_t  *stsd_atom;
  1728.     ngx_http_mp4_trak_t  *trak;

  1729.     /* sample description atom */

  1730.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stsd atom");

  1731.     atom_header = ngx_mp4_atom_header(mp4);
  1732.     stsd_atom = (ngx_mp4_stsd_atom_t *) atom_header;
  1733.     atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
  1734.     atom_table = atom_header + atom_size;
  1735.     ngx_mp4_set_32value(stsd_atom->size, atom_size);
  1736.     ngx_mp4_set_atom_name(stsd_atom, 's', 't', 's', 'd');

  1737.     if (ngx_mp4_atom_data_size(ngx_mp4_stsd_atom_t) > atom_data_size) {
  1738.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1739.                       "\"%s\" mp4 stsd atom too small", mp4->file.name.data);
  1740.         return NGX_ERROR;
  1741.     }

  1742.     ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1743.                    "stsd entries:%uD, media:%*s",
  1744.                    ngx_mp4_get_32value(stsd_atom->entries),
  1745.                    (size_t) 4, stsd_atom->media_name);

  1746.     trak = ngx_mp4_last_trak(mp4);

  1747.     if (trak->out[NGX_HTTP_MP4_STSD_ATOM].buf) {
  1748.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1749.                       "duplicate mp4 stsd atom in \"%s\"", mp4->file.name.data);
  1750.         return NGX_ERROR;
  1751.     }

  1752.     atom = &trak->stsd_atom_buf;
  1753.     atom->temporary = 1;
  1754.     atom->pos = atom_header;
  1755.     atom->last = atom_table;

  1756.     trak->out[NGX_HTTP_MP4_STSD_ATOM].buf = atom;
  1757.     trak->size += atom_size;

  1758.     ngx_mp4_atom_next(mp4, atom_data_size);

  1759.     return NGX_OK;
  1760. }


  1761. typedef struct {
  1762.     u_char    size[4];
  1763.     u_char    name[4];
  1764.     u_char    version[1];
  1765.     u_char    flags[3];
  1766.     u_char    entries[4];
  1767. } ngx_mp4_stts_atom_t;

  1768. typedef struct {
  1769.     u_char    count[4];
  1770.     u_char    duration[4];
  1771. } ngx_mp4_stts_entry_t;


  1772. static ngx_int_t
  1773. ngx_http_mp4_read_stts_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1774. {
  1775.     u_char               *atom_header, *atom_table, *atom_end;
  1776.     uint32_t              entries;
  1777.     ngx_buf_t            *atom, *data;
  1778.     ngx_mp4_stts_atom_t  *stts_atom;
  1779.     ngx_http_mp4_trak_t  *trak;

  1780.     /* time-to-sample atom */

  1781.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stts atom");

  1782.     atom_header = ngx_mp4_atom_header(mp4);
  1783.     stts_atom = (ngx_mp4_stts_atom_t *) atom_header;
  1784.     ngx_mp4_set_atom_name(stts_atom, 's', 't', 't', 's');

  1785.     if (ngx_mp4_atom_data_size(ngx_mp4_stts_atom_t) > atom_data_size) {
  1786.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1787.                       "\"%s\" mp4 stts atom too small", mp4->file.name.data);
  1788.         return NGX_ERROR;
  1789.     }

  1790.     entries = ngx_mp4_get_32value(stts_atom->entries);

  1791.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1792.                    "mp4 time-to-sample entries:%uD", entries);

  1793.     if (ngx_mp4_atom_data_size(ngx_mp4_stts_atom_t)
  1794.         + (uint64_t) entries * sizeof(ngx_mp4_stts_entry_t) > atom_data_size)
  1795.     {
  1796.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1797.                       "\"%s\" mp4 stts atom too small", mp4->file.name.data);
  1798.         return NGX_ERROR;
  1799.     }

  1800.     atom_table = atom_header + sizeof(ngx_mp4_stts_atom_t);
  1801.     atom_end = atom_table + entries * sizeof(ngx_mp4_stts_entry_t);

  1802.     trak = ngx_mp4_last_trak(mp4);

  1803.     if (trak->out[NGX_HTTP_MP4_STTS_ATOM].buf) {
  1804.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1805.                       "duplicate mp4 stts atom in \"%s\"", mp4->file.name.data);
  1806.         return NGX_ERROR;
  1807.     }

  1808.     trak->time_to_sample_entries = entries;

  1809.     atom = &trak->stts_atom_buf;
  1810.     atom->temporary = 1;
  1811.     atom->pos = atom_header;
  1812.     atom->last = atom_table;

  1813.     data = &trak->stts_data_buf;
  1814.     data->temporary = 1;
  1815.     data->pos = atom_table;
  1816.     data->last = atom_end;

  1817.     trak->out[NGX_HTTP_MP4_STTS_ATOM].buf = atom;
  1818.     trak->out[NGX_HTTP_MP4_STTS_DATA].buf = data;

  1819.     ngx_mp4_atom_next(mp4, atom_data_size);

  1820.     return NGX_OK;
  1821. }


  1822. static ngx_int_t
  1823. ngx_http_mp4_update_stts_atom(ngx_http_mp4_file_t *mp4,
  1824.     ngx_http_mp4_trak_t *trak)
  1825. {
  1826.     size_t                atom_size;
  1827.     ngx_buf_t            *atom, *data;
  1828.     ngx_mp4_stts_atom_t  *stts_atom;

  1829.     /*
  1830.      * mdia.minf.stbl.stts updating requires trak->timescale
  1831.      * from mdia.mdhd atom which may reside after mdia.minf
  1832.      */

  1833.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1834.                    "mp4 stts atom update");

  1835.     data = trak->out[NGX_HTTP_MP4_STTS_DATA].buf;

  1836.     if (data == NULL) {
  1837.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1838.                       "no mp4 stts atoms were found in \"%s\"",
  1839.                       mp4->file.name.data);
  1840.         return NGX_ERROR;
  1841.     }

  1842.     if (ngx_http_mp4_crop_stts_data(mp4, trak, 1) != NGX_OK) {
  1843.         return NGX_ERROR;
  1844.     }

  1845.     if (ngx_http_mp4_crop_stts_data(mp4, trak, 0) != NGX_OK) {
  1846.         return NGX_ERROR;
  1847.     }

  1848.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1849.                    "time-to-sample entries:%uD", trak->time_to_sample_entries);

  1850.     atom_size = sizeof(ngx_mp4_stts_atom_t) + (data->last - data->pos);
  1851.     trak->size += atom_size;

  1852.     atom = trak->out[NGX_HTTP_MP4_STTS_ATOM].buf;
  1853.     stts_atom = (ngx_mp4_stts_atom_t *) atom->pos;
  1854.     ngx_mp4_set_32value(stts_atom->size, atom_size);
  1855.     ngx_mp4_set_32value(stts_atom->entries, trak->time_to_sample_entries);

  1856.     return NGX_OK;
  1857. }


  1858. static ngx_int_t
  1859. ngx_http_mp4_crop_stts_data(ngx_http_mp4_file_t *mp4,
  1860.     ngx_http_mp4_trak_t *trak, ngx_uint_t start)
  1861. {
  1862.     uint32_t               count, duration, rest, key_prefix;
  1863.     uint64_t               start_time;
  1864.     ngx_buf_t             *data;
  1865.     ngx_uint_t             start_sample, entries, start_sec;
  1866.     ngx_mp4_stts_entry_t  *entry, *end;

  1867.     if (start) {
  1868.         start_sec = mp4->start;

  1869.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1870.                        "mp4 stts crop start_time:%ui", start_sec);

  1871.     } else if (mp4->length) {
  1872.         start_sec = mp4->length;

  1873.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1874.                        "mp4 stts crop end_time:%ui", start_sec);

  1875.     } else {
  1876.         return NGX_OK;
  1877.     }

  1878.     data = trak->out[NGX_HTTP_MP4_STTS_DATA].buf;

  1879.     start_time = (uint64_t) start_sec * trak->timescale / 1000 + trak->prefix;

  1880.     entries = trak->time_to_sample_entries;
  1881.     start_sample = 0;
  1882.     entry = (ngx_mp4_stts_entry_t *) data->pos;
  1883.     end = (ngx_mp4_stts_entry_t *) data->last;

  1884.     while (entry < end) {
  1885.         count = ngx_mp4_get_32value(entry->count);
  1886.         duration = ngx_mp4_get_32value(entry->duration);

  1887.         ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1888.                        "time:%uL, count:%uD, duration:%uD",
  1889.                        start_time, count, duration);

  1890.         if (start_time < (uint64_t) count * duration) {
  1891.             start_sample += (ngx_uint_t) (start_time / duration);
  1892.             rest = (uint32_t) (start_time / duration);
  1893.             goto found;
  1894.         }

  1895.         start_sample += count;
  1896.         start_time -= (uint64_t) count * duration;
  1897.         entries--;
  1898.         entry++;
  1899.     }

  1900.     if (start) {
  1901.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1902.                       "start time is out mp4 stts samples in \"%s\"",
  1903.                       mp4->file.name.data);

  1904.         return NGX_ERROR;

  1905.     } else {
  1906.         trak->end_sample = trak->start_sample + start_sample;

  1907.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1908.                        "end_sample:%ui", trak->end_sample);

  1909.         return NGX_OK;
  1910.     }

  1911. found:

  1912.     if (start) {
  1913.         if (ngx_http_mp4_seek_key_frame(mp4, trak, start_sample, &key_prefix)
  1914.             != NGX_OK)
  1915.         {
  1916.             return NGX_ERROR;
  1917.         }

  1918.         start_sample -= key_prefix;

  1919.         while (rest < key_prefix) {
  1920.             trak->prefix += rest * duration;
  1921.             key_prefix -= rest;

  1922.             entry--;
  1923.             entries++;

  1924.             count = ngx_mp4_get_32value(entry->count);
  1925.             duration = ngx_mp4_get_32value(entry->duration);
  1926.             rest = count;
  1927.         }

  1928.         trak->prefix += key_prefix * duration;
  1929.         trak->duration += trak->prefix;
  1930.         rest -= key_prefix;

  1931.         ngx_mp4_set_32value(entry->count, count - rest);
  1932.         data->pos = (u_char *) entry;
  1933.         trak->time_to_sample_entries = entries;
  1934.         trak->start_sample = start_sample;

  1935.         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1936.                        "start_sample:%ui, new count:%uD",
  1937.                        trak->start_sample, count - rest);

  1938.     } else {
  1939.         ngx_mp4_set_32value(entry->count, rest);
  1940.         data->last = (u_char *) (entry + 1);
  1941.         trak->time_to_sample_entries -= entries - 1;
  1942.         trak->end_sample = trak->start_sample + start_sample;

  1943.         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1944.                        "end_sample:%ui, new count:%uD",
  1945.                        trak->end_sample, rest);
  1946.     }

  1947.     return NGX_OK;
  1948. }


  1949. static ngx_int_t
  1950. ngx_http_mp4_seek_key_frame(ngx_http_mp4_file_t *mp4, ngx_http_mp4_trak_t *trak,
  1951.     uint32_t start_sample, uint32_t *key_prefix)
  1952. {
  1953.     uint32_t              sample, *entry, *end;
  1954.     ngx_buf_t            *data;
  1955.     ngx_http_mp4_conf_t  *conf;

  1956.     *key_prefix = 0;

  1957.     conf = ngx_http_get_module_loc_conf(mp4->request, ngx_http_mp4_module);
  1958.     if (!conf->start_key_frame) {
  1959.         return NGX_OK;
  1960.     }

  1961.     data = trak->out[NGX_HTTP_MP4_STSS_DATA].buf;
  1962.     if (data == NULL) {
  1963.         return NGX_OK;
  1964.     }

  1965.     entry = (uint32_t *) data->pos;
  1966.     end = (uint32_t *) data->last;

  1967.     /* sync samples starts from 1 */
  1968.     start_sample++;

  1969.     while (entry < end) {
  1970.         sample = ngx_mp4_get_32value(entry);

  1971.         if (sample == 0) {
  1972.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  1973.                           "zero sync sample in \"%s\"",
  1974.                           mp4->file.name.data);
  1975.             return NGX_ERROR;
  1976.         }

  1977.         if (sample > start_sample) {
  1978.             break;
  1979.         }

  1980.         *key_prefix = start_sample - sample;
  1981.         entry++;
  1982.     }

  1983.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  1984.                    "mp4 key frame prefix:%uD", *key_prefix);

  1985.     return NGX_OK;
  1986. }


  1987. typedef struct {
  1988.     u_char    size[4];
  1989.     u_char    name[4];
  1990.     u_char    version[1];
  1991.     u_char    flags[3];
  1992.     u_char    entries[4];
  1993. } ngx_http_mp4_stss_atom_t;


  1994. static ngx_int_t
  1995. ngx_http_mp4_read_stss_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  1996. {
  1997.     u_char                    *atom_header, *atom_table, *atom_end;
  1998.     uint32_t                   entries;
  1999.     ngx_buf_t                 *atom, *data;
  2000.     ngx_http_mp4_trak_t       *trak;
  2001.     ngx_http_mp4_stss_atom_t  *stss_atom;

  2002.     /* sync samples atom */

  2003.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stss atom");

  2004.     atom_header = ngx_mp4_atom_header(mp4);
  2005.     stss_atom = (ngx_http_mp4_stss_atom_t *) atom_header;
  2006.     ngx_mp4_set_atom_name(stss_atom, 's', 't', 's', 's');

  2007.     if (ngx_mp4_atom_data_size(ngx_http_mp4_stss_atom_t) > atom_data_size) {
  2008.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2009.                       "\"%s\" mp4 stss atom too small", mp4->file.name.data);
  2010.         return NGX_ERROR;
  2011.     }

  2012.     entries = ngx_mp4_get_32value(stss_atom->entries);

  2013.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2014.                    "sync sample entries:%uD", entries);

  2015.     trak = ngx_mp4_last_trak(mp4);

  2016.     if (trak->out[NGX_HTTP_MP4_STSS_ATOM].buf) {
  2017.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2018.                       "duplicate mp4 stss atom in \"%s\"", mp4->file.name.data);
  2019.         return NGX_ERROR;
  2020.     }

  2021.     trak->sync_samples_entries = entries;

  2022.     atom_table = atom_header + sizeof(ngx_http_mp4_stss_atom_t);

  2023.     atom = &trak->stss_atom_buf;
  2024.     atom->temporary = 1;
  2025.     atom->pos = atom_header;
  2026.     atom->last = atom_table;

  2027.     if (ngx_mp4_atom_data_size(ngx_http_mp4_stss_atom_t)
  2028.         + (uint64_t) entries * sizeof(uint32_t) > atom_data_size)
  2029.     {
  2030.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2031.                       "\"%s\" mp4 stss atom too small", mp4->file.name.data);
  2032.         return NGX_ERROR;
  2033.     }

  2034.     atom_end = atom_table + entries * sizeof(uint32_t);

  2035.     data = &trak->stss_data_buf;
  2036.     data->temporary = 1;
  2037.     data->pos = atom_table;
  2038.     data->last = atom_end;

  2039.     trak->out[NGX_HTTP_MP4_STSS_ATOM].buf = atom;
  2040.     trak->out[NGX_HTTP_MP4_STSS_DATA].buf = data;

  2041.     ngx_mp4_atom_next(mp4, atom_data_size);

  2042.     return NGX_OK;
  2043. }


  2044. static ngx_int_t
  2045. ngx_http_mp4_update_stss_atom(ngx_http_mp4_file_t *mp4,
  2046.     ngx_http_mp4_trak_t *trak)
  2047. {
  2048.     size_t                     atom_size;
  2049.     uint32_t                   sample, start_sample, *entry, *end;
  2050.     ngx_buf_t                 *atom, *data;
  2051.     ngx_http_mp4_stss_atom_t  *stss_atom;

  2052.     /*
  2053.      * mdia.minf.stbl.stss updating requires trak->start_sample
  2054.      * from mdia.minf.stbl.stts which depends on value from mdia.mdhd
  2055.      * atom which may reside after mdia.minf
  2056.      */

  2057.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2058.                    "mp4 stss atom update");

  2059.     data = trak->out[NGX_HTTP_MP4_STSS_DATA].buf;

  2060.     if (data == NULL) {
  2061.         return NGX_OK;
  2062.     }

  2063.     ngx_http_mp4_crop_stss_data(mp4, trak, 1);
  2064.     ngx_http_mp4_crop_stss_data(mp4, trak, 0);

  2065.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2066.                    "sync sample entries:%uD", trak->sync_samples_entries);

  2067.     if (trak->sync_samples_entries) {
  2068.         entry = (uint32_t *) data->pos;
  2069.         end = (uint32_t *) data->last;

  2070.         start_sample = trak->start_sample;

  2071.         while (entry < end) {
  2072.             sample = ngx_mp4_get_32value(entry);
  2073.             sample -= start_sample;
  2074.             ngx_mp4_set_32value(entry, sample);
  2075.             entry++;
  2076.         }

  2077.     } else {
  2078.         trak->out[NGX_HTTP_MP4_STSS_DATA].buf = NULL;
  2079.     }

  2080.     atom_size = sizeof(ngx_http_mp4_stss_atom_t) + (data->last - data->pos);
  2081.     trak->size += atom_size;

  2082.     atom = trak->out[NGX_HTTP_MP4_STSS_ATOM].buf;
  2083.     stss_atom = (ngx_http_mp4_stss_atom_t *) atom->pos;

  2084.     ngx_mp4_set_32value(stss_atom->size, atom_size);
  2085.     ngx_mp4_set_32value(stss_atom->entries, trak->sync_samples_entries);

  2086.     return NGX_OK;
  2087. }


  2088. static void
  2089. ngx_http_mp4_crop_stss_data(ngx_http_mp4_file_t *mp4,
  2090.     ngx_http_mp4_trak_t *trak, ngx_uint_t start)
  2091. {
  2092.     uint32_t     sample, start_sample, *entry, *end;
  2093.     ngx_buf_t   *data;
  2094.     ngx_uint_t   entries;

  2095.     /* sync samples starts from 1 */

  2096.     if (start) {
  2097.         start_sample = trak->start_sample + 1;

  2098.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2099.                        "mp4 stss crop start_sample:%uD", start_sample);

  2100.     } else if (mp4->length) {
  2101.         start_sample = trak->end_sample + 1;

  2102.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2103.                        "mp4 stss crop end_sample:%uD", start_sample);

  2104.     } else {
  2105.         return;
  2106.     }

  2107.     data = trak->out[NGX_HTTP_MP4_STSS_DATA].buf;

  2108.     entries = trak->sync_samples_entries;
  2109.     entry = (uint32_t *) data->pos;
  2110.     end = (uint32_t *) data->last;

  2111.     while (entry < end) {
  2112.         sample = ngx_mp4_get_32value(entry);

  2113.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2114.                        "sync:%uD", sample);

  2115.         if (sample >= start_sample) {
  2116.             goto found;
  2117.         }

  2118.         entries--;
  2119.         entry++;
  2120.     }

  2121.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2122.                    "sample is out of mp4 stss atom");

  2123. found:

  2124.     if (start) {
  2125.         data->pos = (u_char *) entry;
  2126.         trak->sync_samples_entries = entries;

  2127.     } else {
  2128.         data->last = (u_char *) entry;
  2129.         trak->sync_samples_entries -= entries;
  2130.     }
  2131. }


  2132. typedef struct {
  2133.     u_char    size[4];
  2134.     u_char    name[4];
  2135.     u_char    version[1];
  2136.     u_char    flags[3];
  2137.     u_char    entries[4];
  2138. } ngx_mp4_ctts_atom_t;

  2139. typedef struct {
  2140.     u_char    count[4];
  2141.     u_char    offset[4];
  2142. } ngx_mp4_ctts_entry_t;


  2143. static ngx_int_t
  2144. ngx_http_mp4_read_ctts_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  2145. {
  2146.     u_char               *atom_header, *atom_table, *atom_end;
  2147.     uint32_t              entries;
  2148.     ngx_buf_t            *atom, *data;
  2149.     ngx_mp4_ctts_atom_t  *ctts_atom;
  2150.     ngx_http_mp4_trak_t  *trak;

  2151.     /* composition offsets atom */

  2152.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 ctts atom");

  2153.     atom_header = ngx_mp4_atom_header(mp4);
  2154.     ctts_atom = (ngx_mp4_ctts_atom_t *) atom_header;
  2155.     ngx_mp4_set_atom_name(ctts_atom, 'c', 't', 't', 's');

  2156.     if (ngx_mp4_atom_data_size(ngx_mp4_ctts_atom_t) > atom_data_size) {
  2157.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2158.                       "\"%s\" mp4 ctts atom too small", mp4->file.name.data);
  2159.         return NGX_ERROR;
  2160.     }

  2161.     entries = ngx_mp4_get_32value(ctts_atom->entries);

  2162.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2163.                    "composition offset entries:%uD", entries);

  2164.     trak = ngx_mp4_last_trak(mp4);

  2165.     if (trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf) {
  2166.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2167.                       "duplicate mp4 ctts atom in \"%s\"", mp4->file.name.data);
  2168.         return NGX_ERROR;
  2169.     }

  2170.     trak->composition_offset_entries = entries;

  2171.     atom_table = atom_header + sizeof(ngx_mp4_ctts_atom_t);

  2172.     atom = &trak->ctts_atom_buf;
  2173.     atom->temporary = 1;
  2174.     atom->pos = atom_header;
  2175.     atom->last = atom_table;

  2176.     if (ngx_mp4_atom_data_size(ngx_mp4_ctts_atom_t)
  2177.         + (uint64_t) entries * sizeof(ngx_mp4_ctts_entry_t) > atom_data_size)
  2178.     {
  2179.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2180.                       "\"%s\" mp4 ctts atom too small", mp4->file.name.data);
  2181.         return NGX_ERROR;
  2182.     }

  2183.     atom_end = atom_table + entries * sizeof(ngx_mp4_ctts_entry_t);

  2184.     data = &trak->ctts_data_buf;
  2185.     data->temporary = 1;
  2186.     data->pos = atom_table;
  2187.     data->last = atom_end;

  2188.     trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf = atom;
  2189.     trak->out[NGX_HTTP_MP4_CTTS_DATA].buf = data;

  2190.     ngx_mp4_atom_next(mp4, atom_data_size);

  2191.     return NGX_OK;
  2192. }


  2193. static void
  2194. ngx_http_mp4_update_ctts_atom(ngx_http_mp4_file_t *mp4,
  2195.     ngx_http_mp4_trak_t *trak)
  2196. {
  2197.     size_t                atom_size;
  2198.     ngx_buf_t            *atom, *data;
  2199.     ngx_mp4_ctts_atom_t  *ctts_atom;

  2200.     /*
  2201.      * mdia.minf.stbl.ctts updating requires trak->start_sample
  2202.      * from mdia.minf.stbl.stts which depends on value from mdia.mdhd
  2203.      * atom which may reside after mdia.minf
  2204.      */

  2205.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2206.                    "mp4 ctts atom update");

  2207.     data = trak->out[NGX_HTTP_MP4_CTTS_DATA].buf;

  2208.     if (data == NULL) {
  2209.         return;
  2210.     }

  2211.     ngx_http_mp4_crop_ctts_data(mp4, trak, 1);
  2212.     ngx_http_mp4_crop_ctts_data(mp4, trak, 0);

  2213.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2214.                    "composition offset entries:%uD",
  2215.                    trak->composition_offset_entries);

  2216.     if (trak->composition_offset_entries == 0) {
  2217.         trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf = NULL;
  2218.         trak->out[NGX_HTTP_MP4_CTTS_DATA].buf = NULL;
  2219.         return;
  2220.     }

  2221.     atom_size = sizeof(ngx_mp4_ctts_atom_t) + (data->last - data->pos);
  2222.     trak->size += atom_size;

  2223.     atom = trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf;
  2224.     ctts_atom = (ngx_mp4_ctts_atom_t *) atom->pos;

  2225.     ngx_mp4_set_32value(ctts_atom->size, atom_size);
  2226.     ngx_mp4_set_32value(ctts_atom->entries, trak->composition_offset_entries);

  2227.     return;
  2228. }


  2229. static void
  2230. ngx_http_mp4_crop_ctts_data(ngx_http_mp4_file_t *mp4,
  2231.     ngx_http_mp4_trak_t *trak, ngx_uint_t start)
  2232. {
  2233.     uint32_t               count, start_sample, rest;
  2234.     ngx_buf_t             *data;
  2235.     ngx_uint_t             entries;
  2236.     ngx_mp4_ctts_entry_t  *entry, *end;

  2237.     /* sync samples starts from 1 */

  2238.     if (start) {
  2239.         start_sample = trak->start_sample + 1;

  2240.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2241.                        "mp4 ctts crop start_sample:%uD", start_sample);

  2242.     } else if (mp4->length) {
  2243.         start_sample = trak->end_sample - trak->start_sample + 1;

  2244.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2245.                        "mp4 ctts crop end_sample:%uD", start_sample);

  2246.     } else {
  2247.         return;
  2248.     }

  2249.     data = trak->out[NGX_HTTP_MP4_CTTS_DATA].buf;

  2250.     entries = trak->composition_offset_entries;
  2251.     entry = (ngx_mp4_ctts_entry_t *) data->pos;
  2252.     end = (ngx_mp4_ctts_entry_t *) data->last;

  2253.     while (entry < end) {
  2254.         count = ngx_mp4_get_32value(entry->count);

  2255.         ngx_log_debug3(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2256.                        "sample:%uD, count:%uD, offset:%uD",
  2257.                        start_sample, count, ngx_mp4_get_32value(entry->offset));

  2258.         if (start_sample <= count) {
  2259.             rest = start_sample - 1;
  2260.             goto found;
  2261.         }

  2262.         start_sample -= count;
  2263.         entries--;
  2264.         entry++;
  2265.     }

  2266.     if (start) {
  2267.         data->pos = (u_char *) end;
  2268.         trak->composition_offset_entries = 0;
  2269.     }

  2270.     return;

  2271. found:

  2272.     if (start) {
  2273.         ngx_mp4_set_32value(entry->count, count - rest);
  2274.         data->pos = (u_char *) entry;
  2275.         trak->composition_offset_entries = entries;

  2276.     } else {
  2277.         ngx_mp4_set_32value(entry->count, rest);
  2278.         data->last = (u_char *) (entry + 1);
  2279.         trak->composition_offset_entries -= entries - 1;
  2280.     }
  2281. }


  2282. typedef struct {
  2283.     u_char    size[4];
  2284.     u_char    name[4];
  2285.     u_char    version[1];
  2286.     u_char    flags[3];
  2287.     u_char    entries[4];
  2288. } ngx_mp4_stsc_atom_t;


  2289. static ngx_int_t
  2290. ngx_http_mp4_read_stsc_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  2291. {
  2292.     u_char               *atom_header, *atom_table, *atom_end;
  2293.     uint32_t              entries;
  2294.     ngx_buf_t            *atom, *data;
  2295.     ngx_mp4_stsc_atom_t  *stsc_atom;
  2296.     ngx_http_mp4_trak_t  *trak;

  2297.     /* sample-to-chunk atom */

  2298.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stsc atom");

  2299.     atom_header = ngx_mp4_atom_header(mp4);
  2300.     stsc_atom = (ngx_mp4_stsc_atom_t *) atom_header;
  2301.     ngx_mp4_set_atom_name(stsc_atom, 's', 't', 's', 'c');

  2302.     if (ngx_mp4_atom_data_size(ngx_mp4_stsc_atom_t) > atom_data_size) {
  2303.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2304.                       "\"%s\" mp4 stsc atom too small", mp4->file.name.data);
  2305.         return NGX_ERROR;
  2306.     }

  2307.     entries = ngx_mp4_get_32value(stsc_atom->entries);

  2308.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2309.                    "sample-to-chunk entries:%uD", entries);

  2310.     if (ngx_mp4_atom_data_size(ngx_mp4_stsc_atom_t)
  2311.         + (uint64_t) entries * sizeof(ngx_mp4_stsc_entry_t) > atom_data_size)
  2312.     {
  2313.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2314.                       "\"%s\" mp4 stsc atom too small", mp4->file.name.data);
  2315.         return NGX_ERROR;
  2316.     }

  2317.     atom_table = atom_header + sizeof(ngx_mp4_stsc_atom_t);
  2318.     atom_end = atom_table + entries * sizeof(ngx_mp4_stsc_entry_t);

  2319.     trak = ngx_mp4_last_trak(mp4);

  2320.     if (trak->out[NGX_HTTP_MP4_STSC_ATOM].buf) {
  2321.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2322.                       "duplicate mp4 stsc atom in \"%s\"", mp4->file.name.data);
  2323.         return NGX_ERROR;
  2324.     }

  2325.     trak->sample_to_chunk_entries = entries;

  2326.     atom = &trak->stsc_atom_buf;
  2327.     atom->temporary = 1;
  2328.     atom->pos = atom_header;
  2329.     atom->last = atom_table;

  2330.     data = &trak->stsc_data_buf;
  2331.     data->temporary = 1;
  2332.     data->pos = atom_table;
  2333.     data->last = atom_end;

  2334.     trak->out[NGX_HTTP_MP4_STSC_ATOM].buf = atom;
  2335.     trak->out[NGX_HTTP_MP4_STSC_DATA].buf = data;

  2336.     ngx_mp4_atom_next(mp4, atom_data_size);

  2337.     return NGX_OK;
  2338. }


  2339. static ngx_int_t
  2340. ngx_http_mp4_update_stsc_atom(ngx_http_mp4_file_t *mp4,
  2341.     ngx_http_mp4_trak_t *trak)
  2342. {
  2343.     size_t                 atom_size;
  2344.     uint32_t               chunk;
  2345.     ngx_buf_t             *atom, *data;
  2346.     ngx_mp4_stsc_atom_t   *stsc_atom;
  2347.     ngx_mp4_stsc_entry_t  *entry, *end;

  2348.     /*
  2349.      * mdia.minf.stbl.stsc updating requires trak->start_sample
  2350.      * from mdia.minf.stbl.stts which depends on value from mdia.mdhd
  2351.      * atom which may reside after mdia.minf
  2352.      */

  2353.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2354.                    "mp4 stsc atom update");

  2355.     data = trak->out[NGX_HTTP_MP4_STSC_DATA].buf;

  2356.     if (data == NULL) {
  2357.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2358.                       "no mp4 stsc atoms were found in \"%s\"",
  2359.                       mp4->file.name.data);
  2360.         return NGX_ERROR;
  2361.     }

  2362.     if (trak->sample_to_chunk_entries == 0) {
  2363.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2364.                       "zero number of entries in stsc atom in \"%s\"",
  2365.                       mp4->file.name.data);
  2366.         return NGX_ERROR;
  2367.     }

  2368.     if (ngx_http_mp4_crop_stsc_data(mp4, trak, 1) != NGX_OK) {
  2369.         return NGX_ERROR;
  2370.     }

  2371.     if (ngx_http_mp4_crop_stsc_data(mp4, trak, 0) != NGX_OK) {
  2372.         return NGX_ERROR;
  2373.     }

  2374.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2375.                    "sample-to-chunk entries:%uD",
  2376.                    trak->sample_to_chunk_entries);

  2377.     entry = (ngx_mp4_stsc_entry_t *) data->pos;
  2378.     end = (ngx_mp4_stsc_entry_t *) data->last;

  2379.     while (entry < end) {
  2380.         chunk = ngx_mp4_get_32value(entry->chunk);
  2381.         chunk -= trak->start_chunk;
  2382.         ngx_mp4_set_32value(entry->chunk, chunk);
  2383.         entry++;
  2384.     }

  2385.     atom_size = sizeof(ngx_mp4_stsc_atom_t)
  2386.                 + trak->sample_to_chunk_entries * sizeof(ngx_mp4_stsc_entry_t);

  2387.     trak->size += atom_size;

  2388.     atom = trak->out[NGX_HTTP_MP4_STSC_ATOM].buf;
  2389.     stsc_atom = (ngx_mp4_stsc_atom_t *) atom->pos;

  2390.     ngx_mp4_set_32value(stsc_atom->size, atom_size);
  2391.     ngx_mp4_set_32value(stsc_atom->entries, trak->sample_to_chunk_entries);

  2392.     return NGX_OK;
  2393. }


  2394. static ngx_int_t
  2395. ngx_http_mp4_crop_stsc_data(ngx_http_mp4_file_t *mp4,
  2396.     ngx_http_mp4_trak_t *trak, ngx_uint_t start)
  2397. {
  2398.     uint64_t               n;
  2399.     uint32_t               start_sample, chunk, samples, id, next_chunk,
  2400.                            prev_samples;
  2401.     ngx_buf_t             *data, *buf;
  2402.     ngx_uint_t             entries, target_chunk, chunk_samples;
  2403.     ngx_mp4_stsc_entry_t  *entry, *end, *first;

  2404.     entries = trak->sample_to_chunk_entries - 1;

  2405.     if (start) {
  2406.         start_sample = (uint32_t) trak->start_sample;

  2407.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2408.                        "mp4 stsc crop start_sample:%uD", start_sample);

  2409.     } else if (mp4->length) {
  2410.         start_sample = (uint32_t) (trak->end_sample - trak->start_sample);
  2411.         samples = 0;

  2412.         data = trak->out[NGX_HTTP_MP4_STSC_START].buf;

  2413.         if (data) {
  2414.             entry = (ngx_mp4_stsc_entry_t *) data->pos;
  2415.             samples = ngx_mp4_get_32value(entry->samples);
  2416.             entries--;

  2417.             if (samples > start_sample) {
  2418.                 samples = start_sample;
  2419.                 ngx_mp4_set_32value(entry->samples, samples);
  2420.             }

  2421.             start_sample -= samples;
  2422.         }

  2423.         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2424.                        "mp4 stsc crop end_sample:%uD, ext_samples:%uD",
  2425.                        start_sample, samples);

  2426.     } else {
  2427.         return NGX_OK;
  2428.     }

  2429.     data = trak->out[NGX_HTTP_MP4_STSC_DATA].buf;

  2430.     entry = (ngx_mp4_stsc_entry_t *) data->pos;
  2431.     end = (ngx_mp4_stsc_entry_t *) data->last;

  2432.     chunk = ngx_mp4_get_32value(entry->chunk);
  2433.     samples = ngx_mp4_get_32value(entry->samples);
  2434.     id = ngx_mp4_get_32value(entry->id);
  2435.     prev_samples = 0;
  2436.     entry++;

  2437.     while (entry < end) {

  2438.         next_chunk = ngx_mp4_get_32value(entry->chunk);

  2439.         if (next_chunk < chunk) {
  2440.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2441.                           "unordered mp4 stsc chunks in \"%s\"",
  2442.                           mp4->file.name.data);
  2443.             return NGX_ERROR;
  2444.         }

  2445.         ngx_log_debug5(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2446.                        "sample:%uD, chunk:%uD, chunks:%uD, "
  2447.                        "samples:%uD, id:%uD",
  2448.                        start_sample, chunk, next_chunk - chunk, samples, id);

  2449.         n = (uint64_t) (next_chunk - chunk) * samples;

  2450.         if (start_sample < n) {
  2451.             goto found;
  2452.         }

  2453.         start_sample -= n;

  2454.         if (next_chunk > chunk) {
  2455.             prev_samples = samples;
  2456.         }

  2457.         chunk = next_chunk;
  2458.         samples = ngx_mp4_get_32value(entry->samples);
  2459.         id = ngx_mp4_get_32value(entry->id);
  2460.         entries--;
  2461.         entry++;
  2462.     }

  2463.     next_chunk = trak->chunks + 1;

  2464.     if (next_chunk < chunk) {
  2465.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2466.                       "unordered mp4 stsc chunks in \"%s\"",
  2467.                       mp4->file.name.data);
  2468.         return NGX_ERROR;
  2469.     }

  2470.     ngx_log_debug4(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2471.                    "sample:%uD, chunk:%uD, chunks:%uD, samples:%uD",
  2472.                    start_sample, chunk, next_chunk - chunk, samples);

  2473.     n = (uint64_t) (next_chunk - chunk) * samples;

  2474.     if (start_sample > n) {
  2475.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2476.                       "%s time is out mp4 stsc chunks in \"%s\"",
  2477.                       start ? "start" : "end", mp4->file.name.data);
  2478.         return NGX_ERROR;
  2479.     }

  2480. found:

  2481.     entries++;
  2482.     entry--;

  2483.     if (samples == 0) {
  2484.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2485.                       "zero number of samples in \"%s\"",
  2486.                       mp4->file.name.data);
  2487.         return NGX_ERROR;
  2488.     }

  2489.     if (chunk == 0) {
  2490.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2491.                       "zero chunk in \"%s\"", mp4->file.name.data);
  2492.         return NGX_ERROR;
  2493.     }

  2494.     target_chunk = chunk - 1;
  2495.     target_chunk += start_sample / samples;
  2496.     chunk_samples = start_sample % samples;

  2497.     if (start) {
  2498.         data->pos = (u_char *) entry;

  2499.         trak->sample_to_chunk_entries = entries;
  2500.         trak->start_chunk = target_chunk;
  2501.         trak->start_chunk_samples = chunk_samples;

  2502.         ngx_mp4_set_32value(entry->chunk, trak->start_chunk + 1);

  2503.         samples -= chunk_samples;

  2504.         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2505.                        "start_chunk:%ui, start_chunk_samples:%ui",
  2506.                        trak->start_chunk, trak->start_chunk_samples);

  2507.     } else {
  2508.         if (start_sample) {
  2509.             data->last = (u_char *) (entry + 1);
  2510.             trak->sample_to_chunk_entries -= entries - 1;
  2511.             trak->end_chunk_samples = samples;

  2512.         } else {
  2513.             data->last = (u_char *) entry;
  2514.             trak->sample_to_chunk_entries -= entries;
  2515.             trak->end_chunk_samples = prev_samples;
  2516.         }

  2517.         if (chunk_samples) {
  2518.             trak->end_chunk = target_chunk + 1;
  2519.             trak->end_chunk_samples = chunk_samples;

  2520.         } else {
  2521.             trak->end_chunk = target_chunk;
  2522.         }

  2523.         samples = chunk_samples;
  2524.         next_chunk = chunk + 1;

  2525.         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2526.                        "end_chunk:%ui, end_chunk_samples:%ui",
  2527.                        trak->end_chunk, trak->end_chunk_samples);
  2528.     }

  2529.     if (chunk_samples && next_chunk - target_chunk == 2) {

  2530.         ngx_mp4_set_32value(entry->samples, samples);

  2531.     } else if (chunk_samples && start) {

  2532.         first = &trak->stsc_start_chunk_entry;
  2533.         ngx_mp4_set_32value(first->chunk, 1);
  2534.         ngx_mp4_set_32value(first->samples, samples);
  2535.         ngx_mp4_set_32value(first->id, id);

  2536.         buf = &trak->stsc_start_chunk_buf;
  2537.         buf->temporary = 1;
  2538.         buf->pos = (u_char *) first;
  2539.         buf->last = (u_char *) first + sizeof(ngx_mp4_stsc_entry_t);

  2540.         trak->out[NGX_HTTP_MP4_STSC_START].buf = buf;

  2541.         ngx_mp4_set_32value(entry->chunk, trak->start_chunk + 2);

  2542.         trak->sample_to_chunk_entries++;

  2543.     } else if (chunk_samples) {

  2544.         first = &trak->stsc_end_chunk_entry;
  2545.         ngx_mp4_set_32value(first->chunk, trak->end_chunk - trak->start_chunk);
  2546.         ngx_mp4_set_32value(first->samples, samples);
  2547.         ngx_mp4_set_32value(first->id, id);

  2548.         buf = &trak->stsc_end_chunk_buf;
  2549.         buf->temporary = 1;
  2550.         buf->pos = (u_char *) first;
  2551.         buf->last = (u_char *) first + sizeof(ngx_mp4_stsc_entry_t);

  2552.         trak->out[NGX_HTTP_MP4_STSC_END].buf = buf;

  2553.         trak->sample_to_chunk_entries++;
  2554.     }

  2555.     return NGX_OK;
  2556. }


  2557. typedef struct {
  2558.     u_char    size[4];
  2559.     u_char    name[4];
  2560.     u_char    version[1];
  2561.     u_char    flags[3];
  2562.     u_char    uniform_size[4];
  2563.     u_char    entries[4];
  2564. } ngx_mp4_stsz_atom_t;


  2565. static ngx_int_t
  2566. ngx_http_mp4_read_stsz_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  2567. {
  2568.     u_char               *atom_header, *atom_table, *atom_end;
  2569.     size_t                atom_size;
  2570.     uint32_t              entries, size;
  2571.     ngx_buf_t            *atom, *data;
  2572.     ngx_mp4_stsz_atom_t  *stsz_atom;
  2573.     ngx_http_mp4_trak_t  *trak;

  2574.     /* sample sizes atom */

  2575.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stsz atom");

  2576.     atom_header = ngx_mp4_atom_header(mp4);
  2577.     stsz_atom = (ngx_mp4_stsz_atom_t *) atom_header;
  2578.     ngx_mp4_set_atom_name(stsz_atom, 's', 't', 's', 'z');

  2579.     if (ngx_mp4_atom_data_size(ngx_mp4_stsz_atom_t) > atom_data_size) {
  2580.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2581.                       "\"%s\" mp4 stsz atom too small", mp4->file.name.data);
  2582.         return NGX_ERROR;
  2583.     }

  2584.     size = ngx_mp4_get_32value(stsz_atom->uniform_size);
  2585.     entries = ngx_mp4_get_32value(stsz_atom->entries);

  2586.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2587.                    "sample uniform size:%uD, entries:%uD", size, entries);

  2588.     trak = ngx_mp4_last_trak(mp4);

  2589.     if (trak->out[NGX_HTTP_MP4_STSZ_ATOM].buf) {
  2590.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2591.                       "duplicate mp4 stsz atom in \"%s\"", mp4->file.name.data);
  2592.         return NGX_ERROR;
  2593.     }

  2594.     trak->sample_sizes_entries = entries;

  2595.     atom_table = atom_header + sizeof(ngx_mp4_stsz_atom_t);

  2596.     atom = &trak->stsz_atom_buf;
  2597.     atom->temporary = 1;
  2598.     atom->pos = atom_header;
  2599.     atom->last = atom_table;

  2600.     trak->out[NGX_HTTP_MP4_STSZ_ATOM].buf = atom;

  2601.     if (size == 0) {
  2602.         if (ngx_mp4_atom_data_size(ngx_mp4_stsz_atom_t)
  2603.             + (uint64_t) entries * sizeof(uint32_t) > atom_data_size)
  2604.         {
  2605.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2606.                           "\"%s\" mp4 stsz atom too small",
  2607.                           mp4->file.name.data);
  2608.             return NGX_ERROR;
  2609.         }

  2610.         atom_end = atom_table + entries * sizeof(uint32_t);

  2611.         data = &trak->stsz_data_buf;
  2612.         data->temporary = 1;
  2613.         data->pos = atom_table;
  2614.         data->last = atom_end;

  2615.         trak->out[NGX_HTTP_MP4_STSZ_DATA].buf = data;

  2616.     } else {
  2617.         /* if size != 0 then all samples are the same size */
  2618.         /* TODO : chunk samples */
  2619.         atom_size = sizeof(ngx_mp4_atom_header_t) + (size_t) atom_data_size;
  2620.         ngx_mp4_set_32value(atom_header, atom_size);
  2621.         trak->size += atom_size;
  2622.     }

  2623.     ngx_mp4_atom_next(mp4, atom_data_size);

  2624.     return NGX_OK;
  2625. }


  2626. static ngx_int_t
  2627. ngx_http_mp4_update_stsz_atom(ngx_http_mp4_file_t *mp4,
  2628.     ngx_http_mp4_trak_t *trak)
  2629. {
  2630.     size_t                atom_size;
  2631.     uint32_t             *pos, *end, entries;
  2632.     ngx_buf_t            *atom, *data;
  2633.     ngx_mp4_stsz_atom_t  *stsz_atom;

  2634.     /*
  2635.      * mdia.minf.stbl.stsz updating requires trak->start_sample
  2636.      * from mdia.minf.stbl.stts which depends on value from mdia.mdhd
  2637.      * atom which may reside after mdia.minf
  2638.      */

  2639.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2640.                    "mp4 stsz atom update");

  2641.     data = trak->out[NGX_HTTP_MP4_STSZ_DATA].buf;

  2642.     if (data) {
  2643.         entries = trak->sample_sizes_entries;

  2644.         if (trak->start_sample >= entries) {
  2645.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2646.                           "start time is out mp4 stsz samples in \"%s\"",
  2647.                           mp4->file.name.data);
  2648.             return NGX_ERROR;
  2649.         }

  2650.         entries -= trak->start_sample;
  2651.         data->pos += trak->start_sample * sizeof(uint32_t);
  2652.         end = (uint32_t *) data->pos;

  2653.         for (pos = end - trak->start_chunk_samples; pos < end; pos++) {
  2654.             trak->start_chunk_samples_size += ngx_mp4_get_32value(pos);
  2655.         }

  2656.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2657.                        "chunk samples sizes:%uL",
  2658.                        trak->start_chunk_samples_size);

  2659.         if (trak->start_chunk_samples_size > (uint64_t) mp4->end) {
  2660.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2661.                           "too large mp4 start samples size in \"%s\"",
  2662.                           mp4->file.name.data);
  2663.             return NGX_ERROR;
  2664.         }

  2665.         if (mp4->length) {
  2666.             if (trak->end_sample - trak->start_sample > entries) {
  2667.                 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2668.                               "end time is out mp4 stsz samples in \"%s\"",
  2669.                               mp4->file.name.data);
  2670.                 return NGX_ERROR;
  2671.             }

  2672.             entries = trak->end_sample - trak->start_sample;
  2673.             data->last = data->pos + entries * sizeof(uint32_t);
  2674.             end = (uint32_t *) data->last;

  2675.             for (pos = end - trak->end_chunk_samples; pos < end; pos++) {
  2676.                 trak->end_chunk_samples_size += ngx_mp4_get_32value(pos);
  2677.             }

  2678.             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2679.                            "mp4 stsz end_chunk_samples_size:%uL",
  2680.                            trak->end_chunk_samples_size);

  2681.             if (trak->end_chunk_samples_size > (uint64_t) mp4->end) {
  2682.                 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2683.                               "too large mp4 end samples size in \"%s\"",
  2684.                               mp4->file.name.data);
  2685.                 return NGX_ERROR;
  2686.             }
  2687.         }

  2688.         atom_size = sizeof(ngx_mp4_stsz_atom_t) + (data->last - data->pos);
  2689.         trak->size += atom_size;

  2690.         atom = trak->out[NGX_HTTP_MP4_STSZ_ATOM].buf;
  2691.         stsz_atom = (ngx_mp4_stsz_atom_t *) atom->pos;

  2692.         ngx_mp4_set_32value(stsz_atom->size, atom_size);
  2693.         ngx_mp4_set_32value(stsz_atom->entries, entries);
  2694.     }

  2695.     return NGX_OK;
  2696. }


  2697. typedef struct {
  2698.     u_char    size[4];
  2699.     u_char    name[4];
  2700.     u_char    version[1];
  2701.     u_char    flags[3];
  2702.     u_char    entries[4];
  2703. } ngx_mp4_stco_atom_t;


  2704. static ngx_int_t
  2705. ngx_http_mp4_read_stco_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  2706. {
  2707.     u_char               *atom_header, *atom_table, *atom_end;
  2708.     uint32_t              entries;
  2709.     ngx_buf_t            *atom, *data;
  2710.     ngx_mp4_stco_atom_t  *stco_atom;
  2711.     ngx_http_mp4_trak_t  *trak;

  2712.     /* chunk offsets atom */

  2713.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 stco atom");

  2714.     atom_header = ngx_mp4_atom_header(mp4);
  2715.     stco_atom = (ngx_mp4_stco_atom_t *) atom_header;
  2716.     ngx_mp4_set_atom_name(stco_atom, 's', 't', 'c', 'o');

  2717.     if (ngx_mp4_atom_data_size(ngx_mp4_stco_atom_t) > atom_data_size) {
  2718.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2719.                       "\"%s\" mp4 stco atom too small", mp4->file.name.data);
  2720.         return NGX_ERROR;
  2721.     }

  2722.     entries = ngx_mp4_get_32value(stco_atom->entries);

  2723.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "chunks:%uD", entries);

  2724.     if (ngx_mp4_atom_data_size(ngx_mp4_stco_atom_t)
  2725.         + (uint64_t) entries * sizeof(uint32_t) > atom_data_size)
  2726.     {
  2727.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2728.                       "\"%s\" mp4 stco atom too small", mp4->file.name.data);
  2729.         return NGX_ERROR;
  2730.     }

  2731.     atom_table = atom_header + sizeof(ngx_mp4_stco_atom_t);
  2732.     atom_end = atom_table + entries * sizeof(uint32_t);

  2733.     trak = ngx_mp4_last_trak(mp4);

  2734.     if (trak->out[NGX_HTTP_MP4_STCO_ATOM].buf
  2735.         || trak->out[NGX_HTTP_MP4_CO64_ATOM].buf)
  2736.     {
  2737.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2738.                       "duplicate mp4 stco/co64 atom in \"%s\"",
  2739.                       mp4->file.name.data);
  2740.         return NGX_ERROR;
  2741.     }

  2742.     trak->chunks = entries;

  2743.     atom = &trak->stco_atom_buf;
  2744.     atom->temporary = 1;
  2745.     atom->pos = atom_header;
  2746.     atom->last = atom_table;

  2747.     data = &trak->stco_data_buf;
  2748.     data->temporary = 1;
  2749.     data->pos = atom_table;
  2750.     data->last = atom_end;

  2751.     trak->out[NGX_HTTP_MP4_STCO_ATOM].buf = atom;
  2752.     trak->out[NGX_HTTP_MP4_STCO_DATA].buf = data;

  2753.     ngx_mp4_atom_next(mp4, atom_data_size);

  2754.     return NGX_OK;
  2755. }


  2756. static ngx_int_t
  2757. ngx_http_mp4_update_stco_atom(ngx_http_mp4_file_t *mp4,
  2758.     ngx_http_mp4_trak_t *trak)
  2759. {
  2760.     size_t                atom_size;
  2761.     uint32_t              entries;
  2762.     uint64_t              chunk_offset, samples_size;
  2763.     ngx_buf_t            *atom, *data;
  2764.     ngx_mp4_stco_atom_t  *stco_atom;

  2765.     /*
  2766.      * mdia.minf.stbl.stco updating requires trak->start_chunk
  2767.      * from mdia.minf.stbl.stsc which depends on value from mdia.mdhd
  2768.      * atom which may reside after mdia.minf
  2769.      */

  2770.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2771.                    "mp4 stco atom update");

  2772.     data = trak->out[NGX_HTTP_MP4_STCO_DATA].buf;

  2773.     if (data == NULL) {
  2774.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2775.                       "no mp4 stco atoms were found in \"%s\"",
  2776.                       mp4->file.name.data);
  2777.         return NGX_ERROR;
  2778.     }

  2779.     if (trak->start_chunk >= trak->chunks) {
  2780.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2781.                       "start time is out mp4 stco chunks in \"%s\"",
  2782.                       mp4->file.name.data);
  2783.         return NGX_ERROR;
  2784.     }

  2785.     data->pos += trak->start_chunk * sizeof(uint32_t);

  2786.     chunk_offset = ngx_mp4_get_32value(data->pos);
  2787.     samples_size = trak->start_chunk_samples_size;

  2788.     if (chunk_offset > (uint64_t) mp4->end - samples_size
  2789.         || chunk_offset + samples_size > NGX_MAX_UINT32_VALUE)
  2790.     {
  2791.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2792.                       "too large chunk offset in \"%s\"",
  2793.                       mp4->file.name.data);
  2794.         return NGX_ERROR;
  2795.     }

  2796.     trak->start_offset = chunk_offset + samples_size;
  2797.     ngx_mp4_set_32value(data->pos, trak->start_offset);

  2798.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2799.                    "start chunk offset:%O", trak->start_offset);

  2800.     if (mp4->length) {

  2801.         if (trak->end_chunk > trak->chunks) {
  2802.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2803.                           "end time is out mp4 stco chunks in \"%s\"",
  2804.                           mp4->file.name.data);
  2805.             return NGX_ERROR;
  2806.         }

  2807.         entries = trak->end_chunk - trak->start_chunk;
  2808.         data->last = data->pos + entries * sizeof(uint32_t);

  2809.         if (entries) {
  2810.             chunk_offset = ngx_mp4_get_32value(data->last - sizeof(uint32_t));
  2811.             samples_size = trak->end_chunk_samples_size;

  2812.             if (chunk_offset > (uint64_t) mp4->end - samples_size
  2813.                 || chunk_offset + samples_size > NGX_MAX_UINT32_VALUE)
  2814.             {
  2815.                 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2816.                               "too large chunk offset in \"%s\"",
  2817.                               mp4->file.name.data);
  2818.                 return NGX_ERROR;
  2819.             }

  2820.             trak->end_offset = chunk_offset + samples_size;

  2821.             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2822.                            "end chunk offset:%O", trak->end_offset);
  2823.         }

  2824.     } else {
  2825.         entries = trak->chunks - trak->start_chunk;
  2826.         trak->end_offset = mp4->mdat_data.buf->file_last;
  2827.     }

  2828.     if (entries == 0) {
  2829.         trak->start_offset = mp4->end;
  2830.         trak->end_offset = 0;
  2831.     }

  2832.     atom_size = sizeof(ngx_mp4_stco_atom_t) + (data->last - data->pos);
  2833.     trak->size += atom_size;

  2834.     atom = trak->out[NGX_HTTP_MP4_STCO_ATOM].buf;
  2835.     stco_atom = (ngx_mp4_stco_atom_t *) atom->pos;

  2836.     ngx_mp4_set_32value(stco_atom->size, atom_size);
  2837.     ngx_mp4_set_32value(stco_atom->entries, entries);

  2838.     return NGX_OK;
  2839. }


  2840. static void
  2841. ngx_http_mp4_adjust_stco_atom(ngx_http_mp4_file_t *mp4,
  2842.     ngx_http_mp4_trak_t *trak, int32_t adjustment)
  2843. {
  2844.     uint32_t    offset, *entry, *end;
  2845.     ngx_buf_t  *data;

  2846.     /*
  2847.      * moov.trak.mdia.minf.stbl.stco adjustment requires
  2848.      * minimal start offset of all traks and new moov atom size
  2849.      */

  2850.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2851.                    "mp4 stco atom adjustment");

  2852.     data = trak->out[NGX_HTTP_MP4_STCO_DATA].buf;
  2853.     entry = (uint32_t *) data->pos;
  2854.     end = (uint32_t *) data->last;

  2855.     while (entry < end) {
  2856.         offset = ngx_mp4_get_32value(entry);
  2857.         offset += adjustment;
  2858.         ngx_mp4_set_32value(entry, offset);
  2859.         entry++;
  2860.     }
  2861. }


  2862. typedef struct {
  2863.     u_char    size[4];
  2864.     u_char    name[4];
  2865.     u_char    version[1];
  2866.     u_char    flags[3];
  2867.     u_char    entries[4];
  2868. } ngx_mp4_co64_atom_t;


  2869. static ngx_int_t
  2870. ngx_http_mp4_read_co64_atom(ngx_http_mp4_file_t *mp4, uint64_t atom_data_size)
  2871. {
  2872.     u_char               *atom_header, *atom_table, *atom_end;
  2873.     uint32_t              entries;
  2874.     ngx_buf_t            *atom, *data;
  2875.     ngx_mp4_co64_atom_t  *co64_atom;
  2876.     ngx_http_mp4_trak_t  *trak;

  2877.     /* chunk offsets atom */

  2878.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "mp4 co64 atom");

  2879.     atom_header = ngx_mp4_atom_header(mp4);
  2880.     co64_atom = (ngx_mp4_co64_atom_t *) atom_header;
  2881.     ngx_mp4_set_atom_name(co64_atom, 'c', 'o', '6', '4');

  2882.     if (ngx_mp4_atom_data_size(ngx_mp4_co64_atom_t) > atom_data_size) {
  2883.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2884.                       "\"%s\" mp4 co64 atom too small", mp4->file.name.data);
  2885.         return NGX_ERROR;
  2886.     }

  2887.     entries = ngx_mp4_get_32value(co64_atom->entries);

  2888.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, "chunks:%uD", entries);

  2889.     if (ngx_mp4_atom_data_size(ngx_mp4_co64_atom_t)
  2890.         + (uint64_t) entries * sizeof(uint64_t) > atom_data_size)
  2891.     {
  2892.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2893.                       "\"%s\" mp4 co64 atom too small", mp4->file.name.data);
  2894.         return NGX_ERROR;
  2895.     }

  2896.     atom_table = atom_header + sizeof(ngx_mp4_co64_atom_t);
  2897.     atom_end = atom_table + entries * sizeof(uint64_t);

  2898.     trak = ngx_mp4_last_trak(mp4);

  2899.     if (trak->out[NGX_HTTP_MP4_STCO_ATOM].buf
  2900.         || trak->out[NGX_HTTP_MP4_CO64_ATOM].buf)
  2901.     {
  2902.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2903.                       "duplicate mp4 stco/co64 atom in \"%s\"",
  2904.                       mp4->file.name.data);
  2905.         return NGX_ERROR;
  2906.     }

  2907.     trak->chunks = entries;

  2908.     atom = &trak->co64_atom_buf;
  2909.     atom->temporary = 1;
  2910.     atom->pos = atom_header;
  2911.     atom->last = atom_table;

  2912.     data = &trak->co64_data_buf;
  2913.     data->temporary = 1;
  2914.     data->pos = atom_table;
  2915.     data->last = atom_end;

  2916.     trak->out[NGX_HTTP_MP4_CO64_ATOM].buf = atom;
  2917.     trak->out[NGX_HTTP_MP4_CO64_DATA].buf = data;

  2918.     ngx_mp4_atom_next(mp4, atom_data_size);

  2919.     return NGX_OK;
  2920. }


  2921. static ngx_int_t
  2922. ngx_http_mp4_update_co64_atom(ngx_http_mp4_file_t *mp4,
  2923.     ngx_http_mp4_trak_t *trak)
  2924. {
  2925.     size_t                atom_size;
  2926.     uint64_t              entries, chunk_offset, samples_size;
  2927.     ngx_buf_t            *atom, *data;
  2928.     ngx_mp4_co64_atom_t  *co64_atom;

  2929.     /*
  2930.      * mdia.minf.stbl.co64 updating requires trak->start_chunk
  2931.      * from mdia.minf.stbl.stsc which depends on value from mdia.mdhd
  2932.      * atom which may reside after mdia.minf
  2933.      */

  2934.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2935.                    "mp4 co64 atom update");

  2936.     data = trak->out[NGX_HTTP_MP4_CO64_DATA].buf;

  2937.     if (data == NULL) {
  2938.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2939.                       "no mp4 co64 atoms were found in \"%s\"",
  2940.                       mp4->file.name.data);
  2941.         return NGX_ERROR;
  2942.     }

  2943.     if (trak->start_chunk >= trak->chunks) {
  2944.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2945.                       "start time is out mp4 co64 chunks in \"%s\"",
  2946.                       mp4->file.name.data);
  2947.         return NGX_ERROR;
  2948.     }

  2949.     data->pos += trak->start_chunk * sizeof(uint64_t);

  2950.     chunk_offset = ngx_mp4_get_64value(data->pos);
  2951.     samples_size = trak->start_chunk_samples_size;

  2952.     if (chunk_offset > (uint64_t) mp4->end - samples_size) {
  2953.         ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2954.                       "too large chunk offset in \"%s\"",
  2955.                       mp4->file.name.data);
  2956.         return NGX_ERROR;
  2957.     }

  2958.     trak->start_offset = chunk_offset + samples_size;
  2959.     ngx_mp4_set_64value(data->pos, trak->start_offset);

  2960.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2961.                    "start chunk offset:%O", trak->start_offset);

  2962.     if (mp4->length) {

  2963.         if (trak->end_chunk > trak->chunks) {
  2964.             ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2965.                           "end time is out mp4 co64 chunks in \"%s\"",
  2966.                           mp4->file.name.data);
  2967.             return NGX_ERROR;
  2968.         }

  2969.         entries = trak->end_chunk - trak->start_chunk;
  2970.         data->last = data->pos + entries * sizeof(uint64_t);

  2971.         if (entries) {
  2972.             chunk_offset = ngx_mp4_get_64value(data->last - sizeof(uint64_t));
  2973.             samples_size = trak->end_chunk_samples_size;

  2974.             if (chunk_offset > (uint64_t) mp4->end - samples_size) {
  2975.                 ngx_log_error(NGX_LOG_ERR, mp4->file.log, 0,
  2976.                               "too large chunk offset in \"%s\"",
  2977.                               mp4->file.name.data);
  2978.                 return NGX_ERROR;
  2979.             }

  2980.             trak->end_offset = chunk_offset + samples_size;

  2981.             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  2982.                            "end chunk offset:%O", trak->end_offset);
  2983.         }

  2984.     } else {
  2985.         entries = trak->chunks - trak->start_chunk;
  2986.         trak->end_offset = mp4->mdat_data.buf->file_last;
  2987.     }

  2988.     if (entries == 0) {
  2989.         trak->start_offset = mp4->end;
  2990.         trak->end_offset = 0;
  2991.     }

  2992.     atom_size = sizeof(ngx_mp4_co64_atom_t) + (data->last - data->pos);
  2993.     trak->size += atom_size;

  2994.     atom = trak->out[NGX_HTTP_MP4_CO64_ATOM].buf;
  2995.     co64_atom = (ngx_mp4_co64_atom_t *) atom->pos;

  2996.     ngx_mp4_set_32value(co64_atom->size, atom_size);
  2997.     ngx_mp4_set_32value(co64_atom->entries, entries);

  2998.     return NGX_OK;
  2999. }


  3000. static void
  3001. ngx_http_mp4_adjust_co64_atom(ngx_http_mp4_file_t *mp4,
  3002.     ngx_http_mp4_trak_t *trak, off_t adjustment)
  3003. {
  3004.     uint64_t    offset, *entry, *end;
  3005.     ngx_buf_t  *data;

  3006.     /*
  3007.      * moov.trak.mdia.minf.stbl.co64 adjustment requires
  3008.      * minimal start offset of all traks and new moov atom size
  3009.      */

  3010.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
  3011.                    "mp4 co64 atom adjustment");

  3012.     data = trak->out[NGX_HTTP_MP4_CO64_DATA].buf;
  3013.     entry = (uint64_t *) data->pos;
  3014.     end = (uint64_t *) data->last;

  3015.     while (entry < end) {
  3016.         offset = ngx_mp4_get_64value(entry);
  3017.         offset += adjustment;
  3018.         ngx_mp4_set_64value(entry, offset);
  3019.         entry++;
  3020.     }
  3021. }


  3022. static char *
  3023. ngx_http_mp4(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  3024. {
  3025.     ngx_http_core_loc_conf_t  *clcf;

  3026.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  3027.     clcf->handler = ngx_http_mp4_handler;

  3028.     return NGX_CONF_OK;
  3029. }


  3030. static void *
  3031. ngx_http_mp4_create_conf(ngx_conf_t *cf)
  3032. {
  3033.     ngx_http_mp4_conf_t  *conf;

  3034.     conf = ngx_palloc(cf->pool, sizeof(ngx_http_mp4_conf_t));
  3035.     if (conf == NULL) {
  3036.         return NULL;
  3037.     }

  3038.     conf->buffer_size = NGX_CONF_UNSET_SIZE;
  3039.     conf->max_buffer_size = NGX_CONF_UNSET_SIZE;
  3040.     conf->start_key_frame = NGX_CONF_UNSET;

  3041.     return conf;
  3042. }


  3043. static char *
  3044. ngx_http_mp4_merge_conf(ngx_conf_t *cf, void *parent, void *child)
  3045. {
  3046.     ngx_http_mp4_conf_t *prev = parent;
  3047.     ngx_http_mp4_conf_t *conf = child;

  3048.     ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size, 512 * 1024);
  3049.     ngx_conf_merge_size_value(conf->max_buffer_size, prev->max_buffer_size,
  3050.                               10 * 1024 * 1024);
  3051.     ngx_conf_merge_value(conf->start_key_frame, prev->start_key_frame, 0);

  3052.     return NGX_CONF_OK;
  3053. }