src/http/modules/ngx_http_mp4_module.c - nginx

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 + size <= mp4->buffer_end) {
  851.         return NGX_OK;
  852.     }

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

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

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

  866.         mp4->buffer_start = mp4->buffer;
  867.     }

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

  870.     if (n == NGX_ERROR) {
  871.         return NGX_ERROR;
  872.     }

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

  879.     mp4->buffer_pos = mp4->buffer_start;
  880.     mp4->buffer_end = mp4->buffer_start + mp4->buffer_size;

  881.     return NGX_OK;
  882. }


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

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

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

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

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

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

  908.     ngx_mp4_set_32value(ftyp_atom, atom_size);
  909.     ngx_mp4_set_atom_name(ftyp_atom, 'f', 't', 'y', 'p');

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

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

  920.     mp4->ftyp_atom.buf = atom;
  921.     mp4->ftyp_size = atom_size;
  922.     mp4->content_length = atom_size;

  923.     ngx_mp4_atom_next(mp4, atom_data_size);

  924.     return NGX_OK;
  925. }


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  994.     return rc;
  995. }


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

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

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

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

  1012.     mp4->mdat_atom.buf = &mp4->mdat_atom_buf;
  1013.     mp4->mdat_atom.next = &mp4->mdat_data;
  1014.     mp4->mdat_data.buf = data;

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

  1018.     } else {
  1019.         ngx_mp4_atom_next(mp4, atom_data_size);
  1020.     }

  1021.     return NGX_OK;
  1022. }


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

  1032.     atom_data_size = end_offset - start_offset;
  1033.     mp4->mdat_data.buf->file_pos = start_offset;
  1034.     mp4->mdat_data.buf->file_last = end_offset;

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

  1037.     atom_header = mp4->mdat_atom_header;

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

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

  1050.     ngx_mp4_set_32value(atom_header, atom_size);
  1051.     ngx_mp4_set_atom_name(atom_header, 'm', 'd', 'a', 't');

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

  1056.     return atom_header_size;
  1057. }


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

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


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

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

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

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

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

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

  1129.     } else {
  1130.         /* version 1: 64-bit duration */

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

  1137.         timescale = ngx_mp4_get_32value(mvhd64_atom->timescale);
  1138.         duration = ngx_mp4_get_64value(mvhd64_atom->duration);
  1139.     }

  1140.     mp4->timescale = timescale;

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

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

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

  1151.     duration -= start_time;

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

  1154.         if (duration > length_time) {
  1155.             duration = length_time;
  1156.         }
  1157.     }

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

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

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

  1165.     } else {
  1166.         ngx_mp4_set_64value(mvhd64_atom->duration, duration);
  1167.     }

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

  1172.     mp4->mvhd_atom.buf = atom;

  1173.     ngx_mp4_atom_next(mp4, atom_data_size);

  1174.     return NGX_OK;
  1175. }


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

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

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

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

  1190.     atom_header = ngx_mp4_atom_header(mp4);
  1191.     ngx_mp4_set_atom_name(atom_header, 't', 'r', 'a', 'k');

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

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

  1197.     atom_end = mp4->buffer_pos + (size_t) atom_data_size;
  1198.     atom_file_end = mp4->offset + atom_data_size;

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

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

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

  1210.     return rc;
  1211. }


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

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


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

  1227.     return NGX_ERROR;
  1228. }


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

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


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

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

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

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

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

  1290.     } else {
  1291.         /* version 1: 64-bit duration */

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

  1298.         duration = ngx_mp4_get_64value(tkhd64_atom->duration);
  1299.     }

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

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

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

  1309.     duration -= start_time;

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

  1312.         if (duration > length_time) {
  1313.             duration = length_time;
  1314.         }
  1315.     }

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

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

  1320.     trak = ngx_mp4_last_trak(mp4);

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

  1326.     trak->tkhd_size = atom_size;
  1327.     trak->movie_duration = duration;

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

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

  1331.     } else {
  1332.         ngx_mp4_set_64value(tkhd64_atom->duration, duration);
  1333.     }

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

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

  1339.     ngx_mp4_atom_next(mp4, atom_data_size);

  1340.     return NGX_OK;
  1341. }


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

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

  1349.     atom_header = ngx_mp4_atom_header(mp4);
  1350.     ngx_mp4_set_atom_name(atom_header, 'm', 'd', 'i', 'a');

  1351.     trak = ngx_mp4_last_trak(mp4);

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

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

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

  1362.     return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_mdia_atoms, atom_data_size);
  1363. }


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

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


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

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


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

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

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

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

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

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

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

  1430.         timescale = ngx_mp4_get_32value(mdhd64_atom->timescale);
  1431.         duration = ngx_mp4_get_64value(mdhd64_atom->duration);
  1432.     }

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

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

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

  1442.     duration -= start_time;

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

  1445.         if (duration > length_time) {
  1446.             duration = length_time;
  1447.         }
  1448.     }

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

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

  1453.     trak = ngx_mp4_last_trak(mp4);

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

  1459.     trak->mdhd_size = atom_size;
  1460.     trak->timescale = timescale;
  1461.     trak->duration = duration;

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

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

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

  1468.     ngx_mp4_atom_next(mp4, atom_data_size);

  1469.     return NGX_OK;
  1470. }


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

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

  1482.     mdhd_atom = (ngx_mp4_mdhd_atom_t *) atom->pos;
  1483.     mdhd64_atom = (ngx_mp4_mdhd64_atom_t *) atom->pos;

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

  1486.     } else {
  1487.         ngx_mp4_set_64value(mdhd64_atom->duration, trak->duration);
  1488.     }

  1489.     trak->size += trak->mdhd_size;
  1490. }


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

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

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

  1503.     trak = ngx_mp4_last_trak(mp4);

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

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

  1513.     trak->hdlr_size = atom_size;
  1514.     trak->out[NGX_HTTP_MP4_HDLR_ATOM].buf = atom;

  1515.     ngx_mp4_atom_next(mp4, atom_data_size);

  1516.     return NGX_OK;
  1517. }


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

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

  1525.     atom_header = ngx_mp4_atom_header(mp4);
  1526.     ngx_mp4_set_atom_name(atom_header, 'm', 'i', 'n', 'f');

  1527.     trak = ngx_mp4_last_trak(mp4);

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

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

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

  1538.     return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_minf_atoms, atom_data_size);
  1539. }


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

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


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

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

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

  1564.     trak = ngx_mp4_last_trak(mp4);

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

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

  1577.     trak->vmhd_size += atom_size;
  1578.     trak->out[NGX_HTTP_MP4_VMHD_ATOM].buf = atom;

  1579.     ngx_mp4_atom_next(mp4, atom_data_size);

  1580.     return NGX_OK;
  1581. }


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

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

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

  1594.     trak = ngx_mp4_last_trak(mp4);

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

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

  1607.     trak->smhd_size += atom_size;
  1608.     trak->out[NGX_HTTP_MP4_SMHD_ATOM].buf = atom;

  1609.     ngx_mp4_atom_next(mp4, atom_data_size);

  1610.     return NGX_OK;
  1611. }


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

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

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

  1624.     trak = ngx_mp4_last_trak(mp4);

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

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

  1634.     trak->dinf_size += atom_size;
  1635.     trak->out[NGX_HTTP_MP4_DINF_ATOM].buf = atom;

  1636.     ngx_mp4_atom_next(mp4, atom_data_size);

  1637.     return NGX_OK;
  1638. }


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

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

  1646.     atom_header = ngx_mp4_atom_header(mp4);
  1647.     ngx_mp4_set_atom_name(atom_header, 's', 't', 'b', 'l');

  1648.     trak = ngx_mp4_last_trak(mp4);

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

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

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

  1659.     return ngx_http_mp4_read_atom(mp4, ngx_http_mp4_stbl_atoms, atom_data_size);
  1660. }


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

  1668.     if (trak->prefix == 0) {
  1669.         return;
  1670.     }

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

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

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

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

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

  1685.     elst_atom->version[0] = 1;
  1686.     elst_atom->flags[0] = 0;
  1687.     elst_atom->flags[1] = 0;
  1688.     elst_atom->flags[2] = 0;

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

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

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

  1699.     trak->size += sizeof(ngx_mp4_edts_atom_t) + sizeof(ngx_mp4_elst_atom_t);
  1700. }


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

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


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

  1716.     u_char    media_size[4];
  1717.     u_char    media_name[4];
  1718. } ngx_mp4_stsd_atom_t;


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

  1727.     /* sample description atom */

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

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

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

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

  1744.     trak = ngx_mp4_last_trak(mp4);

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

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

  1754.     trak->out[NGX_HTTP_MP4_STSD_ATOM].buf = atom;
  1755.     trak->size += atom_size;

  1756.     ngx_mp4_atom_next(mp4, atom_data_size);

  1757.     return NGX_OK;
  1758. }


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

  1766. typedef struct {
  1767.     u_char    count[4];
  1768.     u_char    duration[4];
  1769. } ngx_mp4_stts_entry_t;


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

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

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

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

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

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

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

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

  1798.     atom_table = atom_header + sizeof(ngx_mp4_stts_atom_t);
  1799.     atom_end = atom_table + entries * sizeof(ngx_mp4_stts_entry_t);

  1800.     trak = ngx_mp4_last_trak(mp4);

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

  1806.     trak->time_to_sample_entries = entries;

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

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

  1815.     trak->out[NGX_HTTP_MP4_STTS_ATOM].buf = atom;
  1816.     trak->out[NGX_HTTP_MP4_STTS_DATA].buf = data;

  1817.     ngx_mp4_atom_next(mp4, atom_data_size);

  1818.     return NGX_OK;
  1819. }


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

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

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

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

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

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

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

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

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

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

  1854.     return NGX_OK;
  1855. }


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

  1865.     if (start) {
  1866.         start_sec = mp4->start;

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

  1869.     } else if (mp4->length) {
  1870.         start_sec = mp4->length;

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

  1873.     } else {
  1874.         return NGX_OK;
  1875.     }

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

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

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

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

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

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

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

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

  1902.         return NGX_ERROR;

  1903.     } else {
  1904.         trak->end_sample = trak->start_sample + start_sample;

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

  1907.         return NGX_OK;
  1908.     }

  1909. found:

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

  1916.         start_sample -= key_prefix;

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

  1920.             entry--;
  1921.             entries++;

  1922.             count = ngx_mp4_get_32value(entry->count);
  1923.             duration = ngx_mp4_get_32value(entry->duration);
  1924.             rest = count;
  1925.         }

  1926.         trak->prefix += key_prefix * duration;
  1927.         trak->duration += trak->prefix;
  1928.         rest -= key_prefix;

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

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

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

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

  1945.     return NGX_OK;
  1946. }


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

  1954.     *key_prefix = 0;

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

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

  1963.     entry = (uint32_t *) data->pos;
  1964.     end = (uint32_t *) data->last;

  1965.     /* sync samples starts from 1 */
  1966.     start_sample++;

  1967.     while (entry < end) {
  1968.         sample = ngx_mp4_get_32value(entry);

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

  1975.         if (sample > start_sample) {
  1976.             break;
  1977.         }

  1978.         *key_prefix = start_sample - sample;
  1979.         entry++;
  1980.     }

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

  1983.     return NGX_OK;
  1984. }


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


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

  2000.     /* sync samples atom */

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

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

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

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

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

  2013.     trak = ngx_mp4_last_trak(mp4);

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

  2019.     trak->sync_samples_entries = entries;

  2020.     atom_table = atom_header + sizeof(ngx_http_mp4_stss_atom_t);

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

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

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

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

  2037.     trak->out[NGX_HTTP_MP4_STSS_ATOM].buf = atom;
  2038.     trak->out[NGX_HTTP_MP4_STSS_DATA].buf = data;

  2039.     ngx_mp4_atom_next(mp4, atom_data_size);

  2040.     return NGX_OK;
  2041. }


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

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

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

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

  2058.     if (data == NULL) {
  2059.         return NGX_OK;
  2060.     }

  2061.     ngx_http_mp4_crop_stss_data(mp4, trak, 1);
  2062.     ngx_http_mp4_crop_stss_data(mp4, trak, 0);

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

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

  2068.         start_sample = trak->start_sample;

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

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

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

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

  2082.     ngx_mp4_set_32value(stss_atom->size, atom_size);
  2083.     ngx_mp4_set_32value(stss_atom->entries, trak->sync_samples_entries);

  2084.     return NGX_OK;
  2085. }


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

  2093.     /* sync samples starts from 1 */

  2094.     if (start) {
  2095.         start_sample = trak->start_sample + 1;

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

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

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

  2102.     } else {
  2103.         return;
  2104.     }

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

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

  2109.     while (entry < end) {
  2110.         sample = ngx_mp4_get_32value(entry);

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

  2113.         if (sample >= start_sample) {
  2114.             goto found;
  2115.         }

  2116.         entries--;
  2117.         entry++;
  2118.     }

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

  2121. found:

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

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


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

  2137. typedef struct {
  2138.     u_char    count[4];
  2139.     u_char    offset[4];
  2140. } ngx_mp4_ctts_entry_t;


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

  2149.     /* composition offsets atom */

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

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

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

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

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

  2162.     trak = ngx_mp4_last_trak(mp4);

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

  2168.     trak->composition_offset_entries = entries;

  2169.     atom_table = atom_header + sizeof(ngx_mp4_ctts_atom_t);

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

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

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

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

  2186.     trak->out[NGX_HTTP_MP4_CTTS_ATOM].buf = atom;
  2187.     trak->out[NGX_HTTP_MP4_CTTS_DATA].buf = data;

  2188.     ngx_mp4_atom_next(mp4, atom_data_size);

  2189.     return NGX_OK;
  2190. }


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

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

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

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

  2206.     if (data == NULL) {
  2207.         return;
  2208.     }

  2209.     ngx_http_mp4_crop_ctts_data(mp4, trak, 1);
  2210.     ngx_http_mp4_crop_ctts_data(mp4, trak, 0);

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

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

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

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

  2223.     ngx_mp4_set_32value(ctts_atom->size, atom_size);
  2224.     ngx_mp4_set_32value(ctts_atom->entries, trak->composition_offset_entries);

  2225.     return;
  2226. }


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

  2235.     /* sync samples starts from 1 */

  2236.     if (start) {
  2237.         start_sample = trak->start_sample + 1;

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

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

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

  2244.     } else {
  2245.         return;
  2246.     }

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

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

  2251.     while (entry < end) {
  2252.         count = ngx_mp4_get_32value(entry->count);

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

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

  2260.         start_sample -= count;
  2261.         entries--;
  2262.         entry++;
  2263.     }

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

  2268.     return;

  2269. found:

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

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


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


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

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

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

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

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

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

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

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

  2315.     atom_table = atom_header + sizeof(ngx_mp4_stsc_atom_t);
  2316.     atom_end = atom_table + entries * sizeof(ngx_mp4_stsc_entry_t);

  2317.     trak = ngx_mp4_last_trak(mp4);

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

  2323.     trak->sample_to_chunk_entries = entries;

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

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

  2332.     trak->out[NGX_HTTP_MP4_STSC_ATOM].buf = atom;
  2333.     trak->out[NGX_HTTP_MP4_STSC_DATA].buf = data;

  2334.     ngx_mp4_atom_next(mp4, atom_data_size);

  2335.     return NGX_OK;
  2336. }


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

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

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

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

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

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

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

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

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

  2375.     entry = (ngx_mp4_stsc_entry_t *) data->pos;
  2376.     end = (ngx_mp4_stsc_entry_t *) data->last;

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

  2383.     atom_size = sizeof(ngx_mp4_stsc_atom_t)
  2384.                 + trak->sample_to_chunk_entries * sizeof(ngx_mp4_stsc_entry_t);

  2385.     trak->size += atom_size;

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

  2388.     ngx_mp4_set_32value(stsc_atom->size, atom_size);
  2389.     ngx_mp4_set_32value(stsc_atom->entries, trak->sample_to_chunk_entries);

  2390.     return NGX_OK;
  2391. }


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

  2402.     entries = trak->sample_to_chunk_entries - 1;

  2403.     if (start) {
  2404.         start_sample = (uint32_t) trak->start_sample;

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

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

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

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

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

  2419.             start_sample -= samples;
  2420.         }

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

  2424.     } else {
  2425.         return NGX_OK;
  2426.     }

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

  2428.     entry = (ngx_mp4_stsc_entry_t *) data->pos;
  2429.     end = (ngx_mp4_stsc_entry_t *) data->last;

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

  2435.     while (entry < end) {

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

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

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

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

  2448.         if (start_sample < n) {
  2449.             goto found;
  2450.         }

  2451.         start_sample -= n;

  2452.         if (next_chunk > chunk) {
  2453.             prev_samples = samples;
  2454.         }

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

  2461.     next_chunk = trak->chunks + 1;

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

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

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

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

  2478. found:

  2479.     entries++;
  2480.     entry--;

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

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

  2492.     target_chunk = chunk - 1;
  2493.     target_chunk += start_sample / samples;
  2494.     chunk_samples = start_sample % samples;

  2495.     if (start) {
  2496.         data->pos = (u_char *) entry;

  2497.         trak->sample_to_chunk_entries = entries;
  2498.         trak->start_chunk = target_chunk;
  2499.         trak->start_chunk_samples = chunk_samples;

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

  2501.         samples -= chunk_samples;

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

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

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

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

  2518.         } else {
  2519.             trak->end_chunk = target_chunk;
  2520.         }

  2521.         samples = chunk_samples;
  2522.         next_chunk = chunk + 1;

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

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

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

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

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

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

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

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

  2540.         trak->sample_to_chunk_entries++;

  2541.     } else if (chunk_samples) {

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

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

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

  2551.         trak->sample_to_chunk_entries++;
  2552.     }

  2553.     return NGX_OK;
  2554. }


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


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

  2572.     /* sample sizes atom */

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

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

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

  2582.     size = ngx_mp4_get_32value(stsz_atom->uniform_size);
  2583.     entries = ngx_mp4_get_32value(stsz_atom->entries);

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

  2586.     trak = ngx_mp4_last_trak(mp4);

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

  2592.     trak->sample_sizes_entries = entries;

  2593.     atom_table = atom_header + sizeof(ngx_mp4_stsz_atom_t);

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

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

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

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

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

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

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

  2621.     ngx_mp4_atom_next(mp4, atom_data_size);

  2622.     return NGX_OK;
  2623. }


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

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

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

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

  2640.     if (data) {
  2641.         entries = trak->sample_sizes_entries;

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

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

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

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

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

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

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

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

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

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

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

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

  2690.         ngx_mp4_set_32value(stsz_atom->size, atom_size);
  2691.         ngx_mp4_set_32value(stsz_atom->entries, entries);
  2692.     }

  2693.     return NGX_OK;
  2694. }


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


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

  2710.     /* chunk offsets atom */

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

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

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

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

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

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

  2729.     atom_table = atom_header + sizeof(ngx_mp4_stco_atom_t);
  2730.     atom_end = atom_table + entries * sizeof(uint32_t);

  2731.     trak = ngx_mp4_last_trak(mp4);

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

  2740.     trak->chunks = entries;

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

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

  2749.     trak->out[NGX_HTTP_MP4_STCO_ATOM].buf = atom;
  2750.     trak->out[NGX_HTTP_MP4_STCO_DATA].buf = data;

  2751.     ngx_mp4_atom_next(mp4, atom_data_size);

  2752.     return NGX_OK;
  2753. }


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

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

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

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

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

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

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

  2784.     chunk_offset = ngx_mp4_get_32value(data->pos);
  2785.     samples_size = trak->start_chunk_samples_size;

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

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

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

  2798.     if (mp4->length) {

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

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

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

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

  2818.             trak->end_offset = chunk_offset + samples_size;

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

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

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

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

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

  2834.     ngx_mp4_set_32value(stco_atom->size, atom_size);
  2835.     ngx_mp4_set_32value(stco_atom->entries, entries);

  2836.     return NGX_OK;
  2837. }


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

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

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

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

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


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


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

  2875.     /* chunk offsets atom */

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

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

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

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

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

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

  2894.     atom_table = atom_header + sizeof(ngx_mp4_co64_atom_t);
  2895.     atom_end = atom_table + entries * sizeof(uint64_t);

  2896.     trak = ngx_mp4_last_trak(mp4);

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

  2905.     trak->chunks = entries;

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

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

  2914.     trak->out[NGX_HTTP_MP4_CO64_ATOM].buf = atom;
  2915.     trak->out[NGX_HTTP_MP4_CO64_DATA].buf = data;

  2916.     ngx_mp4_atom_next(mp4, atom_data_size);

  2917.     return NGX_OK;
  2918. }


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

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

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

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

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

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

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

  2948.     chunk_offset = ngx_mp4_get_64value(data->pos);
  2949.     samples_size = trak->start_chunk_samples_size;

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

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

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

  2960.     if (mp4->length) {

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

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

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

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

  2978.             trak->end_offset = chunk_offset + samples_size;

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

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

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

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

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

  2994.     ngx_mp4_set_32value(co64_atom->size, atom_size);
  2995.     ngx_mp4_set_32value(co64_atom->entries, entries);

  2996.     return NGX_OK;
  2997. }


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

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

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

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

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


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

  3024.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
  3025.     clcf->handler = ngx_http_mp4_handler;

  3026.     return NGX_CONF_OK;
  3027. }


  3028. static void *
  3029. ngx_http_mp4_create_conf(ngx_conf_t *cf)
  3030. {
  3031.     ngx_http_mp4_conf_t  *conf;

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

  3036.     conf->buffer_size = NGX_CONF_UNSET_SIZE;
  3037.     conf->max_buffer_size = NGX_CONF_UNSET_SIZE;
  3038.     conf->start_key_frame = NGX_CONF_UNSET;

  3039.     return conf;
  3040. }


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

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

  3050.     return NGX_CONF_OK;
  3051. }